|
| 1 | +package queuer |
| 2 | + |
| 3 | +import ( |
| 4 | + "queuer/model" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func MockNextIntervalFunc1(start time.Time, currentCount int) time.Time { |
| 13 | + return start.Add(time.Hour * time.Duration(currentCount)) |
| 14 | +} |
| 15 | +func MockNextIntervalFunc2(start time.Time, currentCount int) time.Time { |
| 16 | + return start.Add(time.Hour * time.Duration(currentCount)) |
| 17 | +} |
| 18 | + |
| 19 | +func TestAddNextIntervalFunc(t *testing.T) { |
| 20 | + t.Run("Successfully adds NextIntervalFunc", func(t *testing.T) { |
| 21 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 22 | + |
| 23 | + worker := testQueuer.AddNextIntervalFunc(MockNextIntervalFunc1) |
| 24 | + require.NotNil(t, worker, "Expected worker to be returned after adding NextIntervalFunc") |
| 25 | + assert.Contains(t, testQueuer.worker.AvailableNextIntervalFuncs, "queuer.MockNextIntervalFunc1", "Expected NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 26 | + assert.Equal(t, 1, len(testQueuer.worker.AvailableNextIntervalFuncs), "Expected only one NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("Successfully adds multiple NextIntervalFuncs", func(t *testing.T) { |
| 30 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 31 | + |
| 32 | + testQueuer.AddNextIntervalFunc(MockNextIntervalFunc1) |
| 33 | + worker := testQueuer.AddNextIntervalFunc(MockNextIntervalFunc2) |
| 34 | + require.NotNil(t, worker, "Expected worker to be returned after adding NextIntervalFunc") |
| 35 | + assert.Contains(t, testQueuer.worker.AvailableNextIntervalFuncs, "queuer.MockNextIntervalFunc1", "Expected NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 36 | + assert.Contains(t, testQueuer.worker.AvailableNextIntervalFuncs, "queuer.MockNextIntervalFunc2", "Expected second NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 37 | + assert.Equal(t, 2, len(testQueuer.worker.AvailableNextIntervalFuncs), "Expected two NextIntervalFuncs to be added to worker's AvailableNextIntervalFuncs") |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("Fails to add nil NextIntervalFunc", func(t *testing.T) { |
| 41 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 42 | + |
| 43 | + var worker *model.Worker |
| 44 | + defer func() { |
| 45 | + r := recover() |
| 46 | + require.NotNil(t, r, "Expected panic when adding nil NextIntervalFunc") |
| 47 | + require.Nil(t, worker, "Expected worker to be nil when adding nil NextIntervalFunc") |
| 48 | + }() |
| 49 | + |
| 50 | + worker = testQueuer.AddNextIntervalFunc(nil) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("Fails to add NextIntervalFunc with existing name", func(t *testing.T) { |
| 54 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 55 | + |
| 56 | + var worker *model.Worker |
| 57 | + defer func() { |
| 58 | + r := recover() |
| 59 | + require.NotNil(t, r, "Expected panic when adding NextIntervalFunc with existing name") |
| 60 | + require.Nil(t, worker, "Expected worker to be nil when adding NextIntervalFunc with existing name") |
| 61 | + }() |
| 62 | + |
| 63 | + testQueuer.AddNextIntervalFunc(MockNextIntervalFunc1) |
| 64 | + worker = testQueuer.AddNextIntervalFunc(MockNextIntervalFunc1) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestAddNextIntervalFuncWithName(t *testing.T) { |
| 69 | + t.Run("Successfully adds NextIntervalFunc with name", func(t *testing.T) { |
| 70 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 71 | + |
| 72 | + worker := testQueuer.AddNextIntervalFuncWithName(MockNextIntervalFunc1, "CustomFuncName") |
| 73 | + require.NotNil(t, worker, "Expected worker to be returned after adding NextIntervalFunc with name") |
| 74 | + assert.Contains(t, testQueuer.worker.AvailableNextIntervalFuncs, "CustomFuncName", "Expected NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 75 | + assert.Equal(t, 1, len(testQueuer.worker.AvailableNextIntervalFuncs), "Expected only one NextIntervalFunc to be added to worker's AvailableNextIntervalFuncs") |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("Fails to add nil NextIntervalFunc with name", func(t *testing.T) { |
| 79 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 80 | + |
| 81 | + var worker *model.Worker |
| 82 | + defer func() { |
| 83 | + r := recover() |
| 84 | + require.NotNil(t, r, "Expected panic when adding nil NextIntervalFunc with name") |
| 85 | + require.Nil(t, worker, "Expected worker to be nil when adding nil NextIntervalFunc with name") |
| 86 | + }() |
| 87 | + |
| 88 | + worker = testQueuer.AddNextIntervalFuncWithName(nil, "CustomFuncName") |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("Fails to add NextIntervalFunc with existing name", func(t *testing.T) { |
| 92 | + testQueuer := newQueuerMock("TestQueuer", 100) |
| 93 | + |
| 94 | + var worker *model.Worker |
| 95 | + defer func() { |
| 96 | + r := recover() |
| 97 | + require.NotNil(t, r, "Expected panic when adding NextIntervalFunc with existing name") |
| 98 | + require.Nil(t, worker, "Expected worker to be nil when adding NextIntervalFunc with existing name") |
| 99 | + }() |
| 100 | + |
| 101 | + testQueuer.AddNextIntervalFuncWithName(MockNextIntervalFunc1, "CustomFuncName") |
| 102 | + worker = testQueuer.AddNextIntervalFuncWithName(MockNextIntervalFunc2, "CustomFuncName") |
| 103 | + }) |
| 104 | +} |
0 commit comments