@@ -2,35 +2,52 @@ package sql
22
33import (
44 "context"
5- "reflect"
6- "runtime"
75 "testing"
86 "time"
97
8+ "github.com/stretchr/testify/mock"
109 "github.com/stretchr/testify/require"
1110)
1211
12+ type notificationQueueMock struct {
13+ mock.Mock
14+ done chan struct {}
15+ queue chan * ProjectionNotification
16+ }
17+
18+ func (m * notificationQueueMock ) Start (done chan struct {}, queue chan * ProjectionNotification ) {
19+ m .Called (done , queue )
20+
21+ m .done = done
22+ m .queue = queue
23+ }
24+
25+ func (m * notificationQueueMock ) Queue (ctx context.Context , notification * ProjectionNotification ) error {
26+ args := m .Called (ctx , notification )
27+
28+ m .queue <- notification
29+
30+ return args .Error (0 )
31+ }
32+
33+ func (m * notificationQueueMock ) ReQueue (ctx context.Context , notification * ProjectionNotification ) error {
34+ args := m .Called (ctx , notification )
35+
36+ return args .Error (0 )
37+ }
38+
1339func TestStartProcessor (t * testing.T ) {
1440 testCases := []struct {
1541 title string
1642 notification func () * ProjectionNotification
17- mockHandler func ( * testing. T , * projectionNotificationProcessor , context. CancelFunc ) ProcessHandler
43+ queueMethod string
1844 }{
1945 {
2046 "Handle nil notification" ,
2147 func () * ProjectionNotification {
2248 return nil
2349 },
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- },
50+ "Queue" ,
3451 },
3552 {
3653 "Handle new notification" ,
@@ -40,53 +57,55 @@ func TestStartProcessor(t *testing.T) {
4057 AggregateID : "abc" ,
4158 }
4259 },
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- },
60+ "ReQueue" ,
5361 },
5462 {
5563 "Handle retried notification" ,
5664 func () * ProjectionNotification {
57- now := time .Now ().Add (time .Millisecond * 200 )
5865 return & ProjectionNotification {
5966 No : 1 ,
6067 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
68+ ValidAfter : time .Now ().Add (time .Millisecond * 200 ),
7469 }
7570 },
71+ "ReQueue" ,
7672 },
7773 }
7874
7975 for _ , testCase := range testCases {
8076 t .Run (testCase .title , func (t * testing.T ) {
81- processor , err := newBackgroundProcessor (1 , 1 , nil , nil , 0 )
82- require .Nil (t , err )
77+ bufferSize := 1
78+ queueProcessorsCount := 1
79+ retryDelay := time .Millisecond * 0
8380
81+ queue := make (chan * ProjectionNotification , bufferSize )
82+ done := make (chan struct {})
8483 ctx , cancel := context .WithCancel (context .Background ())
85- processor . queue = make ( chan * ProjectionNotification , processor . queueBuffer )
84+ notification := testCase . notification ( )
8685
87- err = processor .Queue (ctx , testCase .notification ())
88- require .Nil (t , err )
89- processor .startProcessor (ctx , testCase .mockHandler (t , processor , cancel ))
86+ processor , err := newBackgroundProcessor (queueProcessorsCount , bufferSize , nil , nil , retryDelay )
87+ require .NoError (t , err )
88+ processor .done = done
89+ processor .queue = queue
90+
91+ nqMock := & notificationQueueMock {}
92+ nqMock .On ("Start" , mock .Anything , queue ).Return ()
93+ nqMock .On (testCase .queueMethod , ctx , notification ).Return (nil )
94+ nqMock .Start (processor .done , processor .queue )
95+ processor .notificationQueue = nqMock
96+
97+ queue <- notification
98+
99+ handler := func (ctx context.Context , notification * ProjectionNotification , queue ProjectionTrigger ) error {
100+ defer cancel ()
101+
102+ err := queue (ctx , notification )
103+ require .NoError (t , err )
104+
105+ nqMock .AssertExpectations (t )
106+ return nil
107+ }
108+ processor .startProcessor (ctx , handler )
90109 })
91110 }
92111}
0 commit comments