Notify ChangeListener if worker's groups change 16/188716/6 logger
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 7 Sep 2018 22:35:35 +0000 (00:35 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 17 Sep 2018 10:08:42 +0000 (12:08 +0200)
After changing groups of IDLE worker, matcher should be notified
(via ChangeListener interface) to be able to check for match
of pending requests with updated worker.

Change-Id: I94337ac49d07bd9c4d767f517244284ddd75e4f2
Signed-off-by: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
workers/worker_list_test.go
workers/workers.go

index 52523ae..f8d1535 100644 (file)
@@ -552,6 +552,54 @@ var _ = Describe("WorkerList", func() {
                                Expect(wl.workers[worker].Groups).To(BeNil())
                                wl.mutex.RUnlock()
                        })
+                       Describe("SetGroup with ChangeListener", func() {
+                               var ctrl *gomock.Controller
+                               var wc *MockWorkerChange
+
+                               BeforeEach(func() {
+                                       ctrl = gomock.NewController(GinkgoT())
+                                       wc = NewMockWorkerChange(ctrl)
+                                       wl.SetChangeListener(wc)
+                                       Expect(wl.changeListener).To(Equal(wc))
+                               })
+                               AfterEach(func() {
+                                       ctrl.Finish()
+                               })
+
+                               It("should notify changeListener if set and worker's state is IDLE", func() {
+                                       wl.mutex.RLock()
+                                       wl.workers[worker].State = boruta.IDLE
+                                       wl.mutex.RUnlock()
+
+                                       wc.EXPECT().OnWorkerIdle(worker)
+                                       err := wl.SetGroups(worker, nil)
+                                       Expect(err).ToNot(HaveOccurred())
+                               })
+                               It("should not notify changeListener if set and worker's state is other than IDLE", func() {
+                                       for _, state := range []boruta.WorkerState{boruta.MAINTENANCE, boruta.FAIL, boruta.RUN} {
+                                               By(string(state))
+
+                                               wl.mutex.RLock()
+                                               wl.workers[worker].State = state
+                                               wl.mutex.RUnlock()
+
+                                               err := wl.SetGroups(worker, nil)
+                                               Expect(err).ToNot(HaveOccurred())
+                                       }
+                               })
+                       })
+                       It("should not notify changeListener if not set", func() {
+                               for _, state := range []boruta.WorkerState{boruta.MAINTENANCE, boruta.FAIL, boruta.RUN, boruta.IDLE} {
+                                       By(string(state))
+
+                                       wl.mutex.RLock()
+                                       wl.workers[worker].State = state
+                                       wl.mutex.RUnlock()
+
+                                       err := wl.SetGroups(worker, nil)
+                                       Expect(err).ToNot(HaveOccurred())
+                               }
+                       })
                })
 
                Describe("ListWorkers", func() {
index 6ca5392..ea37d9a 100644 (file)
@@ -189,6 +189,9 @@ func (wl *WorkerList) SetGroups(uuid boruta.WorkerUUID, groups boruta.Groups) er
                return ErrWorkerNotFound
        }
        worker.Groups = groups
+       if worker.State == boruta.IDLE && wl.changeListener != nil {
+               wl.changeListener.OnWorkerIdle(uuid)
+       }
        return nil
 }