9f10ed9a324935cafd85f27cccee0d180501cd15
[platform/core/system/edge-orchestration.git] / src / servicemgr / test / mq_test.go
1 package test
2
3 import (
4         "encoding/json"
5         "fmt"
6         "log"
7         "servicemgr"
8         "strconv"
9         "testing"
10
11         z4 "github.com/alecthomas/gozmq"
12 )
13
14 func TestPushMsg(t *testing.T) {
15         msgChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
16         ctlChan := make(chan bool, 1)
17
18         go servicemgr.Push(uint64(1), msgChan, ctlChan)
19         ctx, err := z4.NewContext()
20         if err != nil {
21                 log.Println("Failed to create new context")
22         }
23         defer ctx.Close()
24
25         pullSock, err := ctx.NewSocket(z4.PULL)
26         if err != nil {
27                 t.Logf(err.Error())
28         }
29         defer pullSock.Close()
30
31         pullSock.Bind(fmt.Sprintf("ipc://%d", uint64(1)))
32
33         for index := 0; index < 20; index++ {
34                 msgChan <- servicemgr.MsgFormat{Header: servicemgr.MsgHeader{Type: "Data"}, Body: strconv.Itoa(index)}
35
36                 msg, _ := pullSock.Recv(0)
37
38                 parsedMsg := servicemgr.MsgFormat{}
39                 json.Unmarshal(msg, &parsedMsg)
40
41                 AssertEqualStr(t, parsedMsg.Body.(string), strconv.Itoa(index))
42         }
43
44         ctlChan <- true
45 }
46
47 func TestPullMsg(t *testing.T) {
48         msgChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
49         ctlPushChan := make(chan bool, 1)
50
51         outputChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
52         ctlPullChan := make(chan bool, 1)
53
54         go servicemgr.Pull(uint64(1), outputChan, ctlPullChan)
55         go servicemgr.Push(uint64(1), msgChan, ctlPushChan)
56
57         for index := 0; index < 20; index++ {
58                 msgChan <- servicemgr.MsgFormat{Header: servicemgr.MsgHeader{Type: "Data"}, Body: strconv.Itoa(index)}
59                 select {
60                 case msg := <-outputChan:
61                         AssertEqualStr(t, msg.Body.(string), strconv.Itoa(index))
62                 }
63         }
64
65         ctlPullChan <- true
66         ctlPushChan <- true
67 }