-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuerWorker_test.go
More file actions
166 lines (142 loc) · 7.02 KB
/
queuerWorker_test.go
File metadata and controls
166 lines (142 loc) · 7.02 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package queuer
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/siherrmann/queuer/helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetWorker(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully get worker created by the new queuer", func(t *testing.T) {
retrievedWorker, err := testQueuer.GetWorker(testQueuer.worker.RID)
assert.NoError(t, err, "expected no error when getting worker")
require.NotNil(t, retrievedWorker, "expected worker to be retrieved")
assert.Equal(t, testQueuer.worker.RID, retrievedWorker.RID, "expected retrieved worker RID to match the original worker RID")
})
t.Run("Returns error for non-existent worker", func(t *testing.T) {
nonExistentWorkerRid := uuid.New()
retrievedWorker, err := testQueuer.GetWorker(nonExistentWorkerRid)
assert.Error(t, err, "expected error when getting non-existent worker")
require.Nil(t, retrievedWorker, "expected no worker to be retrieved for non-existent RID")
})
}
func TestGetWorkers(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully get workers starting from the lastId", func(t *testing.T) {
workers, err := testQueuer.GetWorkers(0, 10)
assert.NoError(t, err, "expected no error when getting workers")
require.NotNil(t, workers, "expected workers to be retrieved")
assert.Equal(t, len(workers), 1, "expected one worker to be retrieved")
})
t.Run("Returns error for invalid lastId", func(t *testing.T) {
workers, err := testQueuer.GetWorkers(-1, 10)
require.Error(t, err, "expected error for invalid lastId")
assert.Contains(t, err.Error(), "lastId cannot be negative", "expected error message for negative lastId")
assert.Nil(t, workers, "expected no workers to be retrieved for invalid lastId")
})
t.Run("Returns error for invalid entries", func(t *testing.T) {
workers, err := testQueuer.GetWorkers(0, 0)
require.Error(t, err, "expected error for invalid entries")
assert.Contains(t, err.Error(), "entries must be greater than zero", "expected error message for zero entries")
assert.Nil(t, workers, "expected no workers to be retrieved for zero entries")
})
t.Run("Return empty slice for non existent lastId", func(t *testing.T) {
workers, err := testQueuer.GetWorkers(1000, 10)
assert.NoError(t, err, "expected no error when getting workers with non-existent lastId")
require.Nil(t, workers, "expected no workers to be retrieved for non-existent lastId")
})
}
func TestGetWorkersBySearch(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully search for workers by name", func(t *testing.T) {
// The queuer creates a worker with name "TestQueuer"
workers, err := testQueuer.GetWorkersBySearch("TestQueuer", 0, 10)
assert.NoError(t, err, "expected no error when searching for workers")
require.NotNil(t, workers, "expected workers to be retrieved")
assert.GreaterOrEqual(t, len(workers), 1, "expected at least one worker matching search term")
assert.Equal(t, "TestQueuer", workers[0].Name, "expected worker name to match search term")
})
t.Run("Successfully search for workers by status", func(t *testing.T) {
workers, err := testQueuer.GetWorkersBySearch("READY", 0, 10)
assert.NoError(t, err, "expected no error when searching for workers by status")
require.NotNil(t, workers, "expected workers to be retrieved")
assert.GreaterOrEqual(t, len(workers), 1, "expected at least one worker with READY status")
})
t.Run("Returns empty result for non-matching search", func(t *testing.T) {
workers, err := testQueuer.GetWorkersBySearch("NonExistentWorker", 0, 10)
assert.NoError(t, err, "expected no error when searching with non-matching term")
if workers != nil {
assert.Len(t, workers, 0, "expected empty slice for non-matching search")
}
})
}
func TestGetConnections(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully get connections", func(t *testing.T) {
connections, err := testQueuer.GetConnections()
assert.NoError(t, err, "expected no error when getting connections")
require.NotNil(t, connections, "expected connections to be retrieved")
})
}
func TestStopWorker(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully stop worker", func(t *testing.T) {
err := testQueuer.StopWorker(testQueuer.worker.RID)
assert.NoError(t, err, "expected no error when stopping worker")
// Verify worker status is set to STOPPED
retrievedWorker, err := testQueuer.GetWorker(testQueuer.worker.RID)
assert.NoError(t, err, "expected no error when getting worker")
require.NotNil(t, retrievedWorker, "expected worker to be retrieved")
assert.Equal(t, "STOPPED", retrievedWorker.Status, "expected worker status to be STOPPED")
})
t.Run("Returns error for non-existent worker", func(t *testing.T) {
nonExistentWorkerRid := uuid.New()
err := testQueuer.StopWorker(nonExistentWorkerRid)
assert.Error(t, err, "expected error when stopping non-existent worker")
assert.Contains(t, err.Error(), "getting worker", "expected error message for non-existent worker")
})
}
func TestStopWorkerGracefully(t *testing.T) {
helper.SetTestDatabaseConfigEnvs(t, dbPort)
testQueuer := NewQueuer("TestQueuer", 100)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testQueuer.StartWithoutWorker(ctx, cancel, true)
t.Run("Successfully stop worker gracefully", func(t *testing.T) {
err := testQueuer.StopWorkerGracefully(testQueuer.worker.RID)
assert.NoError(t, err, "expected no error when stopping worker gracefully")
// Verify worker status is set to STOPPING
retrievedWorker, err := testQueuer.GetWorker(testQueuer.worker.RID)
assert.NoError(t, err, "expected no error when getting worker")
require.NotNil(t, retrievedWorker, "expected worker to be retrieved")
assert.Equal(t, "STOPPING", retrievedWorker.Status, "expected worker status to be STOPPING")
})
t.Run("Returns error for non-existent worker", func(t *testing.T) {
nonExistentWorkerRid := uuid.New()
err := testQueuer.StopWorkerGracefully(nonExistentWorkerRid)
assert.Error(t, err, "expected error when stopping non-existent worker gracefully")
assert.Contains(t, err.Error(), "getting worker", "expected error message for non-existent worker")
})
}