Add Notifier implementation with tests 16/162216/2
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Wed, 29 Nov 2017 15:58:33 +0000 (16:58 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 4 Dec 2017 08:19:07 +0000 (09:19 +0100)
Change-Id: Ib37b4dccf043bb8d5b914b14436929b523446afe
Signed-off-by: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
controller/notifier/notifier_suite_test.go [new file with mode: 0644]
controller/notifier/notifierimpl.go [new file with mode: 0644]
controller/notifier/notifierimpl_test.go [new file with mode: 0644]

diff --git a/controller/notifier/notifier_suite_test.go b/controller/notifier/notifier_suite_test.go
new file mode 100644 (file)
index 0000000..8b61f31
--- /dev/null
@@ -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 (file)
index 0000000..d4179ed
--- /dev/null
@@ -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 (file)
index 0000000..5389e05
--- /dev/null
@@ -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,
+                               })))
+                       }
+               })
+       })
+})