2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
23 "github.com/stretchr/testify/assert"
36 func TestLogger(t *testing.T) {
38 data := map[string]interface{}{"messageType": 1001, "ranIp": "10.0.0.3", "ranPort": 879, "ranName": "test1"}
39 b := new(bytes.Buffer)
40 _ = json.NewEncoder(b).Encode(data)
41 req := tests.GetHttpRequest()
42 boo, _ := ioutil.ReadAll(req.Body)
43 log.Debugf("#rmr_c_go_api_test.TestLogger - request header: %v\n; request body: %s\n", req.Header, string(boo))
46 func TestNewMBufSuccess(t *testing.T) {
47 msg := NewMBuf(tests.MessageType, len(tests.DummyPayload), "RanName", &tests.DummyPayload, &tests.DummyXAction)
49 assert.NotEmpty(t, msg.Payload)
50 assert.NotEmpty(t, msg.XAction)
51 assert.Equal(t, msg.MType, tests.MessageType)
52 assert.Equal(t, msg.Meid, "RanName")
53 assert.Equal(t, msg.Len, len(tests.DummyPayload))
55 //TODO check why test failure and TestIsReadySuccess success
56 func TestInitFailure(t *testing.T) {
58 go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
59 time.Sleep(time.Second)
61 t.Errorf("The rmr router is ready, should be not ready")
65 func TestIsReadyFailure(t *testing.T) {
67 go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
68 assert.True(t, msgr == nil || !msgr.IsReady())
71 func TestSendRecvMsgSuccess(t *testing.T) {
74 go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
75 time.Sleep(time.Duration(2) * time.Second)
76 if msgr == nil || !msgr.IsReady() {
77 t.Errorf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - The rmr router is not ready")
80 msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
81 log.Debugf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - Going to send the message: %#v\n", msg)
82 result, err := msgr.SendMsg(msg)
85 assert.NotNil(t, result)
87 msgR, err := msgr.RecvMsg()
90 assert.NotNil(t, msgR)
94 func TestSendMsgRmrInvalidPortError(t *testing.T) {
97 go initRmr(tests.ReadyIntervalSec, "tcp:" + strconv.Itoa(5555), tests.MaxMsgSize, tests.Flags, log)
98 time.Sleep(time.Duration(2) * time.Second)
99 if msgr == nil || !msgr.IsReady() {
100 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - The rmr router is not ready")
103 msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
104 log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - Going to send the message: %#v\n", msg)
105 result, err := msgr.SendMsg(msg)
107 assert.NotNil(t, err)
108 assert.Nil(t, result)
113 func TestSendMsgRmrInvalidMsgNumError(t *testing.T) {
116 go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
117 time.Sleep(time.Duration(2) * time.Second)
118 if msgr == nil || !msgr.IsReady() {
119 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - The rmr router is not ready")
122 msg := NewMBuf(10, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
123 log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - Going to send the message: %#v\n", msg)
124 result, err := msgr.SendMsg(msg)
126 assert.NotNil(t, err)
127 assert.Nil(t, result)
132 func TestIsReadySuccess(t *testing.T) {
135 go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
136 time.Sleep(time.Duration(tests.ReadyIntervalSec))
137 if msgr == nil || !msgr.IsReady() {
138 t.Errorf("#rmr_c_go_api_test.TestIsReadySuccess - The rmr router is not ready")
142 func initRmr(readyIntervalSec int, port string, maxMsgSize int, flags int, log *logger.Logger){
144 msgr = ctx.Init(readyIntervalSec, port, maxMsgSize, flags, log)
147 func initLog(t *testing.T) *logger.Logger {
148 log, err := logger.InitLogger(logger.DebugLevel)
150 t.Errorf("#rmr_c_go_api_test.initLog - failed to initialize logger, error: %s", err)