2019-12-07 05:44:10 +03:00
// Copyright 2019 The Gitea Authors.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package pull
import (
"strconv"
"testing"
"time"
2022-06-13 12:37:59 +03:00
issues_model "code.gitea.io/gitea/models/issues"
2021-11-12 17:36:47 +03:00
"code.gitea.io/gitea/models/unittest"
2020-02-03 02:19:58 +03:00
"code.gitea.io/gitea/modules/queue"
2019-12-07 05:44:10 +03:00
"github.com/stretchr/testify/assert"
)
func TestPullRequest_AddToTaskQueue ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-12-07 05:44:10 +03:00
2020-02-03 02:19:58 +03:00
idChan := make ( chan int64 , 10 )
2022-01-23 00:22:14 +03:00
q , err := queue . NewChannelUniqueQueue ( func ( data ... queue . Data ) [ ] queue . Data {
2020-02-03 02:19:58 +03:00
for _ , datum := range data {
2020-12-25 12:59:32 +03:00
id , _ := strconv . ParseInt ( datum . ( string ) , 10 , 64 )
2020-02-03 02:19:58 +03:00
idChan <- id
}
2022-01-23 00:22:14 +03:00
return nil
2020-02-03 02:19:58 +03:00
} , queue . ChannelUniqueQueueConfiguration {
WorkerPoolConfiguration : queue . WorkerPoolConfiguration {
QueueLength : 10 ,
BatchLength : 1 ,
2022-03-31 20:01:43 +03:00
Name : "temporary-queue" ,
2020-02-03 02:19:58 +03:00
} ,
Workers : 1 ,
} , "" )
assert . NoError ( t , err )
queueShutdown := [ ] func ( ) { }
queueTerminate := [ ] func ( ) { }
2022-05-02 02:54:44 +03:00
prPatchCheckerQueue = q . ( queue . UniqueQueue )
2020-02-03 02:19:58 +03:00
2022-06-13 12:37:59 +03:00
pr := unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { ID : 2 } ) . ( * issues_model . PullRequest )
2019-12-07 05:44:10 +03:00
AddToTaskQueue ( pr )
2020-02-03 02:19:58 +03:00
assert . Eventually ( t , func ( ) bool {
2022-06-13 12:37:59 +03:00
pr = unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { ID : 2 } ) . ( * issues_model . PullRequest )
return pr . Status == issues_model . PullRequestStatusChecking
2020-02-03 02:19:58 +03:00
} , 1 * time . Second , 100 * time . Millisecond )
2022-05-02 02:54:44 +03:00
has , err := prPatchCheckerQueue . Has ( strconv . FormatInt ( pr . ID , 10 ) )
2020-02-03 02:19:58 +03:00
assert . True ( t , has )
assert . NoError ( t , err )
2022-05-02 02:54:44 +03:00
prPatchCheckerQueue . Run ( func ( shutdown func ( ) ) {
2020-02-03 02:19:58 +03:00
queueShutdown = append ( queueShutdown , shutdown )
2021-05-15 17:22:26 +03:00
} , func ( terminate func ( ) ) {
2020-02-03 02:19:58 +03:00
queueTerminate = append ( queueTerminate , terminate )
} )
2019-12-07 05:44:10 +03:00
select {
2020-02-03 02:19:58 +03:00
case id := <- idChan :
assert . EqualValues ( t , pr . ID , id )
2019-12-07 05:44:10 +03:00
case <- time . After ( time . Second ) :
assert . Fail ( t , "Timeout: nothing was added to pullRequestQueue" )
}
2022-05-02 02:54:44 +03:00
has , err = prPatchCheckerQueue . Has ( strconv . FormatInt ( pr . ID , 10 ) )
2020-02-03 02:19:58 +03:00
assert . False ( t , has )
assert . NoError ( t , err )
2022-06-13 12:37:59 +03:00
pr = unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { ID : 2 } ) . ( * issues_model . PullRequest )
assert . Equal ( t , issues_model . PullRequestStatusChecking , pr . Status )
2020-02-03 02:19:58 +03:00
for _ , callback := range queueShutdown {
callback ( )
}
for _ , callback := range queueTerminate {
callback ( )
}
2022-05-02 02:54:44 +03:00
prPatchCheckerQueue = nil
2019-12-07 05:44:10 +03:00
}