mirror of
				https://gitcode.com/gitea/gitea.git
				synced 2025-10-25 12:26:40 +08:00 
			
		
		
		
	 18f26cfbf7
			
		
	
	18f26cfbf7
	
	
	
		
			
			Before there was a "graceful function": RunWithShutdownFns, it's mainly for some modules which doesn't support context. The old queue system doesn't work well with context, so the old queues need it. After the queue refactoring, the new queue works with context well, so, use Golang context as much as possible, the `RunWithShutdownFns` could be removed (replaced by RunWithCancel for context cancel mechanism), the related code could be simplified. This PR also fixes some legacy queue-init problems, eg: * typo : archiver: "unable to create codes indexer queue" => "unable to create repo-archive queue" * no nil check for failed queues, which causes unfriendly panic After this PR, many goroutines could have better display name:  
		
			
				
	
	
		
			69 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2019 The Gitea Authors.
 | |
| // All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package pull
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"strconv"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	issues_model "code.gitea.io/gitea/models/issues"
 | |
| 	"code.gitea.io/gitea/models/unittest"
 | |
| 	"code.gitea.io/gitea/modules/queue"
 | |
| 	"code.gitea.io/gitea/modules/setting"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| )
 | |
| 
 | |
| func TestPullRequest_AddToTaskQueue(t *testing.T) {
 | |
| 	assert.NoError(t, unittest.PrepareTestDatabase())
 | |
| 
 | |
| 	idChan := make(chan int64, 10)
 | |
| 	testHandler := func(items ...string) []string {
 | |
| 		for _, s := range items {
 | |
| 			id, _ := strconv.ParseInt(s, 10, 64)
 | |
| 			idChan <- id
 | |
| 		}
 | |
| 		return nil
 | |
| 	}
 | |
| 
 | |
| 	cfg, err := setting.GetQueueSettings(setting.CfgProvider, "pr_patch_checker")
 | |
| 	assert.NoError(t, err)
 | |
| 	prPatchCheckerQueue, err = queue.NewWorkerPoolQueueWithContext(context.Background(), "pr_patch_checker", cfg, testHandler, true)
 | |
| 	assert.NoError(t, err)
 | |
| 
 | |
| 	pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
 | |
| 	AddToTaskQueue(pr)
 | |
| 
 | |
| 	assert.Eventually(t, func() bool {
 | |
| 		pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
 | |
| 		return pr.Status == issues_model.PullRequestStatusChecking
 | |
| 	}, 1*time.Second, 100*time.Millisecond)
 | |
| 
 | |
| 	has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
 | |
| 	assert.True(t, has)
 | |
| 	assert.NoError(t, err)
 | |
| 
 | |
| 	go prPatchCheckerQueue.Run()
 | |
| 
 | |
| 	select {
 | |
| 	case id := <-idChan:
 | |
| 		assert.EqualValues(t, pr.ID, id)
 | |
| 	case <-time.After(time.Second):
 | |
| 		assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
 | |
| 	}
 | |
| 
 | |
| 	has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
 | |
| 	assert.False(t, has)
 | |
| 	assert.NoError(t, err)
 | |
| 
 | |
| 	pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
 | |
| 	assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
 | |
| 
 | |
| 	prPatchCheckerQueue.ShutdownWait(5 * time.Second)
 | |
| 	prPatchCheckerQueue = nil
 | |
| }
 |