@@ -110,28 +110,80 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) {
110110 },
111111 "should return error if session validation fails" : {
112112 Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) context.Context {
113- cm .API .On ("ValidateSession" , mock .Anything , mock .Anything ).Return (api.AuthSession {}, slackerror .New ("boomsies " ))
113+ cm .API .On ("ValidateSession" , mock .Anything , mock .Anything ).Return (api.AuthSession {}, slackerror .New ("mock_broken_validation " ))
114114 return ctx
115115 },
116- ExpectedError : slackerror .New ("boomsies " ),
116+ ExpectedError : slackerror .New ("mock_broken_validation " ),
117117 },
118- "should return error if activity request fails" : {
118+ "should return error from Activity API request if TailArg is not set" : {
119+ Args : types.ActivityArgs {
120+ TailArg : false ,
121+ },
119122 Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) context.Context {
120- cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).Return (api.ActivityResult {}, slackerror .New ("explosions " ))
123+ cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).Return (api.ActivityResult {}, slackerror .New ("mock_broken_logs " ))
121124 return ctx
122125 },
123- ExpectedError : slackerror .New ("explosions" ),
126+ ExpectedError : slackerror .New ("mock_broken_logs" ),
127+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
128+ cm .API .AssertNumberOfCalls (t , "Activity" , 1 )
129+ },
124130 },
125131 "should return nil and invoke Activity API only once if TailArg is not set" : {
126132 Args : types.ActivityArgs {
127- TailArg : false ,
133+ TailArg : false ,
134+ IdleTimeoutM : 1 ,
135+ PollingIntervalMS : 20 , // poll activity every 20 ms
128136 },
129137 Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) context.Context {
130- cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).Return (api.ActivityResult {}, nil )
138+ cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).
139+ Return (api.ActivityResult {
140+ Activities : []api.Activity {
141+ {
142+ Level : types .WARN ,
143+ ComponentID : "a123" ,
144+ TraceID : "tr123" ,
145+ },
146+ {
147+ Level : types .ERROR ,
148+ ComponentID : "a456" ,
149+ TraceID : "tr456" ,
150+ },
151+ },
152+ }, nil )
153+ ctx , cancel := context .WithCancel (ctx )
154+ go func () {
155+ time .Sleep (time .Millisecond * 50 ) // cancel activity in 50 ms
156+ cancel ()
157+ }()
131158 return ctx
132159 },
160+ ExpectedError : nil ,
133161 ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
162+ // with the above tail argument not included we call the method once.
134163 cm .API .AssertNumberOfCalls (t , "Activity" , 1 )
164+ assert .Contains (t , cm .GetStdoutOutput (), "[warn] [a123] (Trace=tr123)" )
165+ assert .Contains (t , cm .GetStdoutOutput (), "[error] [a456] (Trace=tr456)" )
166+ },
167+ },
168+ "should return nil and invoke Activity API twice if TailArg is set while polling" : {
169+ Args : types.ActivityArgs {
170+ TailArg : true ,
171+ IdleTimeoutM : 0 ,
172+ PollingIntervalMS : 20 , // poll activity every 20 ms
173+ },
174+ Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) context.Context {
175+ cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).Return (api.ActivityResult {}, nil )
176+ ctx , cancel := context .WithCancel (ctx )
177+ go func () {
178+ time .Sleep (time .Millisecond * 50 ) // cancel activity in 50 ms
179+ cancel ()
180+ }()
181+ return ctx
182+ },
183+ ExpectedError : nil ,
184+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
185+ // with the above polling/canceling setup, expectation is activity called two times.
186+ cm .API .AssertNumberOfCalls (t , "Activity" , 2 )
135187 },
136188 },
137189 "should return nil if TailArg is set and context is canceled" : {
@@ -148,11 +200,33 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) {
148200 }()
149201 return ctx
150202 },
203+ ExpectedError : nil ,
151204 ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
152205 // with the above polling/canceling setup, expectation is activity called only once.
153206 cm .API .AssertNumberOfCalls (t , "Activity" , 1 )
154207 },
155208 },
209+ "should return nil if TailArg is set and activity request fails while polling" : {
210+ Args : types.ActivityArgs {
211+ TailArg : true ,
212+ IdleTimeoutM : 1 ,
213+ PollingIntervalMS : 20 , // poll activity every 20 ms
214+ },
215+ Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) context.Context {
216+ cm .API .On ("Activity" , mock .Anything , mock .Anything , mock .Anything ).Return (api.ActivityResult {}, slackerror .New ("mock_broken_logs" ))
217+ ctx , cancel := context .WithCancel (ctx )
218+ go func () {
219+ time .Sleep (time .Millisecond * 50 ) // cancel activity in 50 ms
220+ cancel ()
221+ }()
222+ return ctx
223+ },
224+ ExpectedError : nil ,
225+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
226+ // with the above polling/canceling setup, expectation is activity called three times.
227+ cm .API .AssertNumberOfCalls (t , "Activity" , 3 )
228+ },
229+ },
156230 } {
157231 t .Run (name , func (t * testing.T ) {
158232 // Create mocks
@@ -172,6 +246,7 @@ func TestPlatformActivity_StreamingLogs(t *testing.T) {
172246
173247 err := Activity (ctxMock , clients , & logger.Logger {}, tt .Args )
174248 if tt .ExpectedError != nil {
249+ require .Error (t , err )
175250 assert .Contains (t , err .Error (), tt .ExpectedError .Error (), err )
176251 } else {
177252 require .NoError (t , err )
0 commit comments