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 (
2020-02-03 02:19:58 +03:00
"context"
2019-12-07 05:44:10 +03:00
"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"
2020-02-03 02:19:58 +03:00
"github.com/unknwon/com"
2019-12-07 05:44:10 +03:00
)
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 {
prID := datum . ( string )
id := com . StrTo ( prID ) . MustInt64 ( )
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 )
2019-12-07 05:44:10 +03:00
pr := models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 1 } ) . ( * models . PullRequest )
AddToTaskQueue ( pr )
2020-02-03 02:19:58 +03:00
assert . Eventually ( t , func ( ) bool {
pr = models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 1 } ) . ( * models . PullRequest )
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 )
prQueue . Run ( func ( _ context . Context , shutdown func ( ) ) {
queueShutdown = append ( queueShutdown , shutdown )
} , func ( _ context . Context , terminate func ( ) ) {
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 )
2019-12-07 05:44:10 +03:00
pr = models . AssertExistsAndLoadBean ( t , & models . PullRequest { ID : 1 } ) . ( * models . PullRequest )
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
}