summaryrefslogtreecommitdiff
path: root/services/worker/internal/pool/pool_test.go
blob: f347faf64bd37e508fc4fc6b5e3ab1c140875175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package pool

import (
	"context"
	"github.com/stretchr/testify/assert"
	"log/slog"
	"sync"
	"sync/atomic"
	"testing"
	"time"
)

func newTestLogger() *slog.Logger {
	return slog.New(slog.DiscardHandler)
}

func TestWorkerPoolConcurrencyLimit(test *testing.T) {
	workerPool := NewWorkerPool(2, newTestLogger())
	workerContext := context.Background()

	var peakConcurrency atomic.Int32
	var currentConcurrency atomic.Int32
	var completedJobs atomic.Int32

	for jobIndex := 0; jobIndex < 10; jobIndex++ {
		workerPool.Submit(workerContext, func(workContext context.Context) {
			current := currentConcurrency.Add(1)

			for {
				peak := peakConcurrency.Load()

				if current <= peak || peakConcurrency.CompareAndSwap(peak, current) {
					break
				}
			}

			time.Sleep(10 * time.Millisecond)
			currentConcurrency.Add(-1)
			completedJobs.Add(1)
		})
	}

	workerPool.Wait()
	assert.Equal(test, int32(10), completedJobs.Load())
	assert.LessOrEqual(test, peakConcurrency.Load(), int32(2))
}

func TestWorkerPoolPanicRecovery(test *testing.T) {
	workerPool := NewWorkerPool(2, newTestLogger())
	workerContext := context.Background()

	var completedAfterPanic atomic.Bool

	workerPool.Submit(workerContext, func(workContext context.Context) {
		panic("intentional test panic")
	})
	time.Sleep(20 * time.Millisecond)
	workerPool.Submit(workerContext, func(workContext context.Context) {
		completedAfterPanic.Store(true)
	})
	workerPool.Wait()
	assert.True(test, completedAfterPanic.Load())
}

func TestWorkerPoolCancelledContext(test *testing.T) {
	workerPool := NewWorkerPool(1, newTestLogger())
	cancelledContext, cancelFunction := context.WithCancel(context.Background())

	var blockingMutex sync.Mutex

	blockingMutex.Lock()
	workerPool.Submit(context.Background(), func(workContext context.Context) {
		blockingMutex.Lock()

		defer blockingMutex.Unlock()
	})
	cancelFunction()

	submitted := workerPool.Submit(cancelledContext, func(workContext context.Context) {})

	assert.False(test, submitted)
	blockingMutex.Unlock()
	workerPool.Wait()
}

func TestWorkerPoolWaitBlocksUntilDone(test *testing.T) {
	workerPool := NewWorkerPool(4, newTestLogger())
	workerContext := context.Background()

	var counter atomic.Int32

	for jobIndex := 0; jobIndex < 20; jobIndex++ {
		workerPool.Submit(workerContext, func(workContext context.Context) {
			time.Sleep(5 * time.Millisecond)
			counter.Add(1)
		})
	}

	workerPool.Wait()
	assert.Equal(test, int32(20), counter.Load())
}

func TestWorkerPoolActiveWorkerCount(test *testing.T) {
	workerPool := NewWorkerPool(3, newTestLogger())

	assert.Equal(test, 0, workerPool.ActiveWorkerCount())

	var releaseMutex sync.Mutex

	releaseMutex.Lock()
	workerPool.Submit(context.Background(), func(workContext context.Context) {
		releaseMutex.Lock()

		defer releaseMutex.Unlock()
	})
	workerPool.Submit(context.Background(), func(workContext context.Context) {
		releaseMutex.Lock()

		defer releaseMutex.Unlock()
	})
	time.Sleep(20 * time.Millisecond)
	assert.Equal(test, 2, workerPool.ActiveWorkerCount())
	releaseMutex.Unlock()
	workerPool.Wait()
}