From: Lukasz Wojciechowski Date: Wed, 29 Nov 2017 15:58:33 +0000 (+0100) Subject: Add Notifier implementation with tests X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d8cff28a7f8e7590b7db11658839bfca81a293f2;p=tools%2Fweles.git Add Notifier implementation with tests Change-Id: Ib37b4dccf043bb8d5b914b14436929b523446afe Signed-off-by: Lukasz Wojciechowski --- diff --git a/controller/notifier/notifier_suite_test.go b/controller/notifier/notifier_suite_test.go new file mode 100644 index 0000000..8b61f31 --- /dev/null +++ b/controller/notifier/notifier_suite_test.go @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package notifier + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestNotifier(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Notifier Suite") +} diff --git a/controller/notifier/notifierimpl.go b/controller/notifier/notifierimpl.go new file mode 100644 index 0000000..d4179ed --- /dev/null +++ b/controller/notifier/notifierimpl.go @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +// File controller/notifier/notifierimpl.go implements Notifier interface. +// It creates channel for communication between Controler and Controller's +// internal submodules. Channel is used for notifying Controller about +// either failure or success of Job processing. + +package notifier + +import ( + . "git.tizen.org/tools/weles" +) + +// Impl implements Notifier interface +type Impl struct { + Notifier + channel chan Notification +} + +// NewNotifier creates a new Impl structure setting up channel. +func NewNotifier() Notifier { + ret := &Impl{ + channel: make(chan Notification, BuffSize), + } + return ret +} + +// Listen returns channel which transmits notification about failure +// or success of processing Job. +func (h *Impl) Listen() <-chan Notification { + return h.channel +} + +// SendFail notifies Controller about failure. +func (h *Impl) SendFail(j JobID, msg string) { + h.channel <- Notification{ + JobID: j, + OK: false, + Msg: msg, + } +} + +// SendOK notifies Controller about success. +func (h *Impl) SendOK(j JobID) { + h.channel <- Notification{ + JobID: j, + OK: true, + } +} diff --git a/controller/notifier/notifierimpl_test.go b/controller/notifier/notifierimpl_test.go new file mode 100644 index 0000000..5389e05 --- /dev/null +++ b/controller/notifier/notifierimpl_test.go @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package notifier + +import ( + "fmt" + + . "git.tizen.org/tools/weles" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Impl", func() { + var h Notifier + + BeforeEach(func() { + h = NewNotifier() + }) + Describe("NewNotifier", func() { + It("should create a new object", func() { + Expect(h).NotTo(BeNil()) + Expect(h.(*Impl).channel).NotTo(BeNil()) + }) + }) + Describe("Listen", func() { + It("should return read only channel", func() { + c := h.Listen() + + Expect(c).To(Equal((<-chan Notification)(h.(*Impl).channel))) + }) + }) + Describe("SendFail", func() { + It("should send proper notifications to channel", func() { + for i := 1; i <= 5; i++ { + h.SendFail(JobID(i), fmt.Sprintf("message number %d", i)) + } + + c := h.Listen() + for i := 1; i <= 5; i++ { + Eventually(c).Should(Receive(Equal(Notification{ + JobID: JobID(i), + OK: false, + Msg: fmt.Sprintf("message number %d", i), + }))) + } + }) + }) + Describe("SendOK", func() { + It("should send proper notifications to channel", func() { + for i := 1; i <= 5; i++ { + h.SendOK(JobID(i)) + } + + c := h.Listen() + for i := 1; i <= 5; i++ { + Eventually(c).Should(Receive(Equal(Notification{ + JobID: JobID(i), + OK: true, + }))) + } + }) + }) +})