@@ -425,35 +425,33 @@ var _ = Describe("The slack thread plugin", func() {
425425 var server * slacktest.Server
426426 var url string
427427 var leaseName types.NamespacedName
428+ var rtm * slack.RTM
428429
429430 BeforeEach (func () {
430431 leaseName = types.NamespacedName {
431432 Name : "slack-lease" ,
432433 Namespace : metav1 .NamespaceDefault ,
433434 }
435+
434436 server = slacktest .NewTestServer ()
435437 server .Start ()
436438 url = server .GetAPIURL ()
439+
440+ api := slack .New ("test-token" , slack .OptionAPIURL (url ))
441+ rtm = api .NewRTM ()
442+ go rtm .ManageConnection ()
437443 })
438444
439- AfterEach (func () {
445+ AfterEach (func (ctx SpecContext ) {
446+ err := rtm .Disconnect ()
447+ Expect (err ).To (Succeed ())
440448 lease := & coordinationv1.Lease {}
441- Expect (k8sClient .Get (context . Background () , leaseName , lease )).To (Succeed ())
442- Expect (k8sClient .Delete (context . Background () , lease )).To (Succeed ())
449+ Expect (k8sClient .Get (ctx , leaseName , lease )).To (Succeed ())
450+ Expect (k8sClient .Delete (ctx , lease )).To (Succeed ())
443451 server .Stop ()
444452 })
445453
446- fetchMessages := func (g Gomega ) []slack.Msg {
447- msgs := make ([]slack.Msg , 0 )
448- for _ , outbound := range server .GetSeenOutboundMessages () {
449- msg := slack.Msg {}
450- g .Expect (json .Unmarshal ([]byte (outbound ), & msg )).To (Succeed ())
451- msgs = append (msgs , msg )
452- }
453- return msgs
454- }
455-
456- It ("should send a message and create its lease" , func () {
454+ It ("should send a message and create its lease" , func (ctx SpecContext ) {
457455 thread := impl.SlackThread {
458456 Token : "" ,
459457 Title : "title" ,
@@ -463,20 +461,21 @@ var _ = Describe("The slack thread plugin", func() {
463461 Period : 1 * time .Second ,
464462 }
465463 thread .SetTestURL (url )
466- err := thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : context .Background ()})
467- Expect (err ).To (Succeed ())
468- Eventually (fetchMessages ).Should (SatisfyAll (HaveLen (2 ), Satisfy (func (msgs []slack.Msg ) bool {
469- return msgs [0 ].Timestamp == msgs [1 ].ThreadTimestamp &&
470- msgs [0 ].Text == "title" && msgs [1 ].Text == "msg"
471- })))
464+ Expect (thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : ctx })).To (Succeed ())
465+ var firstMsg slack.RTMEvent
466+ Eventually (rtm .IncomingEvents ).Should (Receive (& firstMsg , HaveField ("Data.Msg.Text" , "title" )))
467+ Eventually (rtm .IncomingEvents ).Should (Receive (SatisfyAll (
468+ HaveField ("Data.Msg.Text" , "msg" ),
469+ HaveField ("Data.Msg.ThreadTimestamp" , firstMsg .Data .(* slack.MessageEvent ).Timestamp ),
470+ )))
472471 Eventually (func () error {
473472 var lease coordinationv1.Lease
474- err := k8sClient .Get (context . Background () , thread .LeaseName , & lease )
473+ err := k8sClient .Get (ctx , thread .LeaseName , & lease )
475474 return err
476475 }).Should (Succeed ())
477476 })
478477
479- It ("should use replies if the lease did not timeout" , func () {
478+ It ("should use replies if the lease did not timeout" , func (ctx SpecContext ) {
480479 thread := impl.SlackThread {
481480 Token : "" ,
482481 Title : "title" ,
@@ -486,16 +485,15 @@ var _ = Describe("The slack thread plugin", func() {
486485 Period : 5 * time .Second ,
487486 }
488487 thread .SetTestURL (url )
489- err := thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : context .Background ()})
490- Expect (err ).To (Succeed ())
491- err = thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : context .Background ()})
492- Expect (err ).To (Succeed ())
493- Eventually (fetchMessages ).Should (SatisfyAll (HaveLen (3 ), Satisfy (func (msgs []slack.Msg ) bool {
494- return msgs [0 ].Timestamp == msgs [1 ].ThreadTimestamp && msgs [0 ].Timestamp == msgs [2 ].ThreadTimestamp
495- })))
488+ Expect (thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : ctx })).To (Succeed ())
489+ Expect (thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : ctx })).To (Succeed ())
490+ var firstMsg slack.RTMEvent
491+ Eventually (rtm .IncomingEvents ).Should (Receive (& firstMsg , HaveField ("Data.Msg.Text" , "title" )))
492+ Eventually (rtm .IncomingEvents ).Should (Receive (HaveField ("Data.Msg.ThreadTimestamp" , firstMsg .Data .(* slack.MessageEvent ).Timestamp )))
493+ Eventually (rtm .IncomingEvents ).Should (Receive (HaveField ("Data.Msg.ThreadTimestamp" , firstMsg .Data .(* slack.MessageEvent ).Timestamp )))
496494 })
497495
498- It ("creates a new thread once the lease times out" , func () {
496+ It ("creates a new thread once the lease times out" , func (ctx SpecContext ) {
499497 thread := impl.SlackThread {
500498 Token : "" ,
501499 Title : "title" ,
@@ -505,14 +503,21 @@ var _ = Describe("The slack thread plugin", func() {
505503 Period : 1 * time .Second ,
506504 }
507505 thread .SetTestURL (url )
508- err := thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : context .Background ()})
509- Expect (err ).To (Succeed ())
510- time .Sleep (2 * time .Second )
511- err = thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : context .Background ()})
512- Expect (err ).To (Succeed ())
513- Eventually (fetchMessages ).Should (SatisfyAll (HaveLen (4 ), Satisfy (func (msgs []slack.Msg ) bool {
514- return msgs [0 ].Timestamp == msgs [1 ].ThreadTimestamp && msgs [2 ].Timestamp == msgs [3 ].ThreadTimestamp
515- })))
506+ Expect (thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : ctx })).To (Succeed ())
507+ time .Sleep (1100 * time .Millisecond )
508+ Expect (thread .Notify (plugin.Parameters {Client : k8sClient , Ctx : ctx })).To (Succeed ())
509+ var firstMsg slack.RTMEvent
510+ Eventually (rtm .IncomingEvents ).Should (Receive (& firstMsg , HaveField ("Data.Msg.Text" , "title" )))
511+ Eventually (rtm .IncomingEvents ).Should (Receive (SatisfyAll (
512+ HaveField ("Data.Msg.Text" , "msg" ),
513+ HaveField ("Data.Msg.ThreadTimestamp" , firstMsg .Data .(* slack.MessageEvent ).Timestamp ),
514+ )))
515+ var secondMsg slack.RTMEvent
516+ Eventually (rtm .IncomingEvents ).Should (Receive (& secondMsg , HaveField ("Data.Msg.Text" , "title" )))
517+ Eventually (rtm .IncomingEvents ).Should (Receive (SatisfyAll (
518+ HaveField ("Data.Msg.Text" , "msg" ),
519+ HaveField ("Data.Msg.ThreadTimestamp" , secondMsg .Data .(* slack.MessageEvent ).Timestamp ),
520+ )))
516521 })
517522})
518523
0 commit comments