From 32cb659630b6b746e0443b0e36e6b872248d0f33 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Sat, 8 Sep 2018 00:35:35 +0200 Subject: [PATCH] Notify ChangeListener if worker's groups change 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 --- workers/worker_list_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ workers/workers.go | 3 +++ 2 files changed, 51 insertions(+) diff --git a/workers/worker_list_test.go b/workers/worker_list_test.go index 52523ae..f8d1535 100644 --- a/workers/worker_list_test.go +++ b/workers/worker_list_test.go @@ -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() { diff --git a/workers/workers.go b/workers/workers.go index 6ca5392..ea37d9a 100644 --- a/workers/workers.go +++ b/workers/workers.go @@ -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 } -- 2.7.4