File: workers_test.go

package info (click to toggle)
golang-github-minio-pkg 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: xml: 37; makefile: 35; asm: 22
file content (148 lines) | stat: -rw-r--r-- 3,061 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2022-2023 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package workers

import (
	"fmt"
	"sync"
	"testing"
)

func TestWorkers(t *testing.T) {
	tests := []struct {
		n        int
		jobs     int
		mustFail bool
	}{
		{
			n:        0,
			jobs:     5,
			mustFail: true,
		},
		{
			n:        -1,
			jobs:     5,
			mustFail: true,
		},
		{
			n:    1,
			jobs: 5,
		},
		{
			n:    2,
			jobs: 5,
		},
		{
			n:    5,
			jobs: 10,
		},
		{
			n:    10,
			jobs: 5,
		},
	}
	testFn := func(t *testing.T, n, jobs int, mustFail bool) {
		var mu sync.Mutex
		var jobsDone int
		// Create workers for n concurrent workers
		jt, err := New(n)
		if err == nil && mustFail {
			t.Fatal("Expected test to return error")
		}
		if err != nil && mustFail {
			return
		}
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		for i := 0; i < jobs; i++ {
			jt.Take()
			go func() { // Launch a worker after acquiring a token
				defer jt.Give() // Give token back once done
				mu.Lock()
				jobsDone++
				mu.Unlock()
			}()
		}
		jt.Wait() // Wait for all workers to complete
		if jobsDone != jobs {
			t.Fatalf("Expected %d jobs to be done but only %d were done", jobs, jobsDone)
		}
	}

	for i, test := range tests {
		t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
			testFn(t, test.n, test.jobs, test.mustFail)
		})
	}

	// Verify that workers can be reused after full drain
	t.Run("test-workers-reuse", func(t *testing.T) {
		var mu sync.Mutex
		jt, _ := New(5)
		for reuse := 0; reuse < 3; reuse++ {
			var jobsDone int
			for i := 0; i < 10; i++ {
				jt.Take()
				go func() {
					defer jt.Give()
					mu.Lock()
					jobsDone++
					mu.Unlock()
				}()
			}
			jt.Wait()
			if jobsDone != 10 {
				t.Fatalf("Expected %d jobs to be complete but only %d were", 10, jobsDone)
			}
		}
	})
}

func benchmarkWorkers(b *testing.B, n, jobs int) {
	b.ReportAllocs()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			var mu sync.Mutex
			var jobsDone int
			jt, _ := New(n)
			for i := 0; i < jobs; i++ {
				jt.Take()
				go func() {
					defer jt.Give()
					mu.Lock()
					jobsDone++
					mu.Unlock()
				}()
			}
			jt.Wait()
			if jobsDone != jobs {
				b.Fail()
			}
		}
	})
}

func BenchmarkWorkers_N5_J10(b *testing.B) {
	benchmarkWorkers(b, 5, 10)
}

func BenchmarkWorkers_N5_J100(b *testing.B) {
	benchmarkWorkers(b, 5, 100)
}