|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestStartProcessor(t *testing.T) { |
| 14 | + testCases := []struct { |
| 15 | + title string |
| 16 | + notification func() *ProjectionNotification |
| 17 | + mockHandler func(*testing.T, *projectionNotificationProcessor, context.CancelFunc) ProcessHandler |
| 18 | + }{ |
| 19 | + { |
| 20 | + "Handle nil notification", |
| 21 | + func() *ProjectionNotification { |
| 22 | + return nil |
| 23 | + }, |
| 24 | + func(t *testing.T, processor *projectionNotificationProcessor, cancel context.CancelFunc) ProcessHandler { |
| 25 | + return func(ctx context.Context, notification *ProjectionNotification, queue ProjectionTrigger) error { |
| 26 | + defer cancel() |
| 27 | + |
| 28 | + expectedFuncName := runtime.FuncForPC(reflect.ValueOf(processor.Queue).Pointer()).Name() |
| 29 | + actualFuncName := runtime.FuncForPC(reflect.ValueOf(queue).Pointer()).Name() |
| 30 | + require.Equal(t, expectedFuncName, actualFuncName) |
| 31 | + return nil |
| 32 | + } |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + "Handle new notification", |
| 37 | + func() *ProjectionNotification { |
| 38 | + return &ProjectionNotification{ |
| 39 | + No: 1, |
| 40 | + AggregateID: "abc", |
| 41 | + } |
| 42 | + }, |
| 43 | + func(t *testing.T, processor *projectionNotificationProcessor, cancel context.CancelFunc) ProcessHandler { |
| 44 | + return func(ctx context.Context, notification *ProjectionNotification, queue ProjectionTrigger) error { |
| 45 | + defer cancel() |
| 46 | + |
| 47 | + expectedFuncName := runtime.FuncForPC(reflect.ValueOf(processor.ReQueue).Pointer()).Name() |
| 48 | + actualFuncName := runtime.FuncForPC(reflect.ValueOf(queue).Pointer()).Name() |
| 49 | + require.Equal(t, expectedFuncName, actualFuncName) |
| 50 | + return nil |
| 51 | + } |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + "Handle retried notification", |
| 56 | + func() *ProjectionNotification { |
| 57 | + now := time.Now().Add(time.Millisecond * 200) |
| 58 | + return &ProjectionNotification{ |
| 59 | + No: 1, |
| 60 | + AggregateID: "abc", |
| 61 | + ValidAfter: now, |
| 62 | + } |
| 63 | + }, |
| 64 | + func(t *testing.T, processor *projectionNotificationProcessor, cancel context.CancelFunc) ProcessHandler { |
| 65 | + return func(ctx context.Context, notification *ProjectionNotification, queue ProjectionTrigger) error { |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + expectedFuncName := runtime.FuncForPC(reflect.ValueOf(processor.ReQueue).Pointer()).Name() |
| 69 | + actualFuncName := runtime.FuncForPC(reflect.ValueOf(queue).Pointer()).Name() |
| 70 | + require.Equal(t, expectedFuncName, actualFuncName) |
| 71 | + |
| 72 | + require.True(t, notification.ValidAfter.Before(time.Now())) |
| 73 | + return nil |
| 74 | + } |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, testCase := range testCases { |
| 80 | + t.Run(testCase.title, func(t *testing.T) { |
| 81 | + processor, err := newBackgroundProcessor(1, 1, nil, nil, 0) |
| 82 | + require.Nil(t, err) |
| 83 | + |
| 84 | + ctx, cancel := context.WithCancel(context.Background()) |
| 85 | + processor.queue = make(chan *ProjectionNotification, processor.queueBuffer) |
| 86 | + |
| 87 | + err = processor.Queue(ctx, testCase.notification()) |
| 88 | + require.Nil(t, err) |
| 89 | + processor.startProcessor(ctx, testCase.mockHandler(t, processor, cancel)) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments