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"
"code.gitea.io/gitea/models"
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 ) {
assert . NoError ( t , models . PrepareTestDatabase ( ) )
2020-02-03 02:19:58 +03:00
idChan := make ( chan int64 , 10 )
q , err := queue . NewChannelUniqueQueue ( func ( data ... queue . Data ) {
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
}
} , queue . ChannelUniqueQueueConfiguration {
WorkerPoolConfiguration : queue . WorkerPoolConfiguration {
QueueLength : 10 ,
BatchLength : 1 ,
} ,
Workers : 1 ,
Name : "temporary-queue" ,
} , "" )
assert . NoError ( t , err )
queueShutdown := [ ] func ( ) { }
queueTerminate := [ ] func ( ) { }
prQueue = q . ( queue . UniqueQueue )
2020-02-10 02:09:31 +03:00
pr := models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 2 } ) . ( * models . PullRequest )
2019-12-07 05:44:10 +03:00
AddToTaskQueue ( pr )
2020-02-03 02:19:58 +03:00
assert . Eventually ( t , func ( ) bool {
2020-02-10 02:09:31 +03:00
pr = models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 2 } ) . ( * models . PullRequest )
2020-02-03 02:19:58 +03:00
return pr . Status == models . PullRequestStatusChecking
} , 1 * time . Second , 100 * time . Millisecond )
has , err := prQueue . Has ( strconv . FormatInt ( pr . ID , 10 ) )
assert . True ( t , has )
assert . NoError ( t , err )
2021-05-15 17:22:26 +03:00
prQueue . 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" )
}
2020-02-03 02:19:58 +03:00
has , err = prQueue . Has ( strconv . FormatInt ( pr . ID , 10 ) )
assert . False ( t , has )
assert . NoError ( t , err )
2020-02-10 02:09:31 +03:00
pr = models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 2 } ) . ( * models . PullRequest )
2019-12-07 05:44:10 +03:00
assert . Equal ( t , models . PullRequestStatusChecking , pr . Status )
2020-02-03 02:19:58 +03:00
for _ , callback := range queueShutdown {
callback ( )
}
for _ , callback := range queueTerminate {
callback ( )
}
prQueue = nil
2019-12-07 05:44:10 +03:00
}