code.gitea.io/gitea@v1.19.3/modules/queue/queue_disk_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package queue
     5  
     6  import (
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestLevelQueue(t *testing.T) {
    15  	handleChan := make(chan *testData)
    16  	handle := func(data ...Data) []Data {
    17  		assert.True(t, len(data) == 2)
    18  		for _, datum := range data {
    19  			testDatum := datum.(*testData)
    20  			handleChan <- testDatum
    21  		}
    22  		return nil
    23  	}
    24  
    25  	var lock sync.Mutex
    26  	queueShutdown := []func(){}
    27  	queueTerminate := []func(){}
    28  
    29  	tmpDir := t.TempDir()
    30  
    31  	queue, err := NewLevelQueue(handle, LevelQueueConfiguration{
    32  		ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{
    33  			WorkerPoolConfiguration: WorkerPoolConfiguration{
    34  				QueueLength:  20,
    35  				BatchLength:  2,
    36  				BlockTimeout: 1 * time.Second,
    37  				BoostTimeout: 5 * time.Minute,
    38  				BoostWorkers: 5,
    39  				MaxWorkers:   10,
    40  			},
    41  			Workers: 1,
    42  		},
    43  		DataDir: tmpDir,
    44  	}, &testData{})
    45  	assert.NoError(t, err)
    46  
    47  	go queue.Run(func(shutdown func()) {
    48  		lock.Lock()
    49  		queueShutdown = append(queueShutdown, shutdown)
    50  		lock.Unlock()
    51  	}, func(terminate func()) {
    52  		lock.Lock()
    53  		queueTerminate = append(queueTerminate, terminate)
    54  		lock.Unlock()
    55  	})
    56  
    57  	test1 := testData{"A", 1}
    58  	test2 := testData{"B", 2}
    59  
    60  	err = queue.Push(&test1)
    61  	assert.NoError(t, err)
    62  	go func() {
    63  		err := queue.Push(&test2)
    64  		assert.NoError(t, err)
    65  	}()
    66  
    67  	result1 := <-handleChan
    68  	assert.Equal(t, test1.TestString, result1.TestString)
    69  	assert.Equal(t, test1.TestInt, result1.TestInt)
    70  
    71  	result2 := <-handleChan
    72  	assert.Equal(t, test2.TestString, result2.TestString)
    73  	assert.Equal(t, test2.TestInt, result2.TestInt)
    74  
    75  	err = queue.Push(test1)
    76  	assert.Error(t, err)
    77  
    78  	lock.Lock()
    79  	for _, callback := range queueShutdown {
    80  		callback()
    81  	}
    82  	lock.Unlock()
    83  
    84  	time.Sleep(200 * time.Millisecond)
    85  	err = queue.Push(&test1)
    86  	assert.NoError(t, err)
    87  	err = queue.Push(&test2)
    88  	assert.NoError(t, err)
    89  	select {
    90  	case <-handleChan:
    91  		assert.Fail(t, "Handler processing should have stopped")
    92  	default:
    93  	}
    94  	lock.Lock()
    95  	for _, callback := range queueTerminate {
    96  		callback()
    97  	}
    98  	lock.Unlock()
    99  
   100  	// Reopen queue
   101  	queue, err = NewWrappedQueue(handle,
   102  		WrappedQueueConfiguration{
   103  			Underlying: LevelQueueType,
   104  			Config: LevelQueueConfiguration{
   105  				ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{
   106  					WorkerPoolConfiguration: WorkerPoolConfiguration{
   107  						QueueLength:  20,
   108  						BatchLength:  2,
   109  						BlockTimeout: 1 * time.Second,
   110  						BoostTimeout: 5 * time.Minute,
   111  						BoostWorkers: 5,
   112  						MaxWorkers:   10,
   113  					},
   114  					Workers: 1,
   115  				},
   116  				DataDir: tmpDir,
   117  			},
   118  		}, &testData{})
   119  	assert.NoError(t, err)
   120  
   121  	go queue.Run(func(shutdown func()) {
   122  		lock.Lock()
   123  		queueShutdown = append(queueShutdown, shutdown)
   124  		lock.Unlock()
   125  	}, func(terminate func()) {
   126  		lock.Lock()
   127  		queueTerminate = append(queueTerminate, terminate)
   128  		lock.Unlock()
   129  	})
   130  
   131  	result3 := <-handleChan
   132  	assert.Equal(t, test1.TestString, result3.TestString)
   133  	assert.Equal(t, test1.TestInt, result3.TestInt)
   134  
   135  	result4 := <-handleChan
   136  	assert.Equal(t, test2.TestString, result4.TestString)
   137  	assert.Equal(t, test2.TestInt, result4.TestInt)
   138  
   139  	lock.Lock()
   140  	for _, callback := range queueShutdown {
   141  		callback()
   142  	}
   143  	for _, callback := range queueTerminate {
   144  		callback()
   145  	}
   146  	lock.Unlock()
   147  }