"os"
"strconv"
"strings"
+ "sync"
"testing"
"time"
)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
-type testingControl struct {
+
+type httpEventWaiter struct {
+ resultChan chan bool
+ nextActionOk bool
+}
+
+func (msg *httpEventWaiter) SetResult(res bool) {
+ msg.resultChan <- res
+}
+
+func (msg *httpEventWaiter) WaitResult(t *testing.T) bool {
+ select {
+ case result := <-msg.resultChan:
+ return result
+ case <-time.After(15 * time.Second):
+ testError(t, "Waiter not received result status from case within 15 secs")
+ return false
+ }
+ testError(t, "Waiter error in default branch")
+ return false
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type testingHttpRtmgrControl struct {
+ sync.Mutex
+ desc string
+ port string
+ eventWaiter *httpEventWaiter
+}
+
+func (hc *testingHttpRtmgrControl) NextEvent(eventWaiter *httpEventWaiter) {
+ hc.Lock()
+ defer hc.Unlock()
+ hc.eventWaiter = eventWaiter
+}
+
+func (hc *testingHttpRtmgrControl) AllocNextEvent(nextAction bool) *httpEventWaiter {
+ eventWaiter := &httpEventWaiter{
+ resultChan: make(chan bool),
+ nextActionOk: nextAction,
+ }
+ hc.NextEvent(eventWaiter)
+ return eventWaiter
+}
+
+func (hc *testingHttpRtmgrControl) http_handler(w http.ResponseWriter, r *http.Request) {
+
+ hc.Lock()
+ defer hc.Unlock()
+
+ var req rtmgr_models.XappSubscriptionData
+ err := json.NewDecoder(r.Body).Decode(&req)
+ if err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ }
+ xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID)
+
+ var code int = 0
+ switch r.Method {
+ case http.MethodPost:
+ code = 201
+ if hc.eventWaiter != nil {
+ if hc.eventWaiter.nextActionOk == false {
+ code = 400
+ }
+ }
+ case http.MethodDelete:
+ code = 200
+ if hc.eventWaiter != nil {
+ if hc.eventWaiter.nextActionOk == false {
+ code = 400
+ }
+ }
+ default:
+ code = 200
+ }
+
+ waiter := hc.eventWaiter
+ hc.eventWaiter = nil
+ if waiter != nil {
+ waiter.SetResult(true)
+ }
+ xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code)
+ w.WriteHeader(code)
+
+}
+
+func (hc *testingHttpRtmgrControl) run() {
+ http.HandleFunc("/", hc.http_handler)
+ http.ListenAndServe("localhost:"+hc.port, nil)
+}
+
+func initTestingHttpRtmgrControl(desc string, port string) *testingHttpRtmgrControl {
+ hc := &testingHttpRtmgrControl{}
+ hc.desc = desc
+ hc.port = port
+ return hc
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type testingRmrControl struct {
desc string
syncChan chan struct{}
}
-func (tc *testingControl) ReadyCB(data interface{}) {
- xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc)
+func (tc *testingRmrControl) ReadyCB(data interface{}) {
+ xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.desc)
tc.syncChan <- struct{}{}
return
}
-func (tc *testingControl) WaitCB() {
+func (tc *testingRmrControl) WaitCB() {
<-tc.syncChan
}
-func initTestingControl(desc string, rtfile string, port string) testingControl {
- tc := testingControl{}
+func initTestingControl(desc string, rtfile string, port string) testingRmrControl {
+ tc := testingRmrControl{}
os.Setenv("RMR_SEED_RT", rtfile)
os.Setenv("RMR_SRC_ID", "localhost:"+port)
xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
-type testingRmrControl struct {
- testingControl
+type testingRmrStubControl struct {
+ testingRmrControl
rmrClientTest *xapp.RMRClient
active bool
+ msgCnt uint64
+}
+
+func (tc *testingRmrStubControl) GetMsgCnt() uint64 {
+ return tc.msgCnt
+}
+
+func (tc *testingRmrStubControl) IncMsgCnt() {
+ tc.msgCnt++
}
-func (tc *testingRmrControl) RmrSend(params *RMRParams) (err error) {
+func (tc *testingRmrStubControl) DecMsgCnt() {
+ if tc.msgCnt > 0 {
+ tc.msgCnt--
+ }
+}
+
+func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) {
+ if tc.GetMsgCnt() > 0 {
+ testError(t, "(%s) message count expected 0 but is %d", tc.desc, tc.GetMsgCnt())
+ }
+}
+
+func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
//
//NOTE: Do this way until xapp-frame sending is improved
//
return
}
-func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrControl {
- tc := testingRmrControl{}
+func initTestingRmrControl(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) testingRmrStubControl {
+ tc := testingRmrStubControl{}
tc.active = false
- tc.testingControl = initTestingControl(desc, rtfile, port)
+ tc.testingRmrControl = initTestingControl(desc, rtfile, port)
tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
go tc.rmrClientTest.Start(consumer)
}
type testingXappControl struct {
- testingRmrControl
+ testingRmrStubControl
testingMessageChannel
xid_seq uint64
}
if strings.Contains(msg.Xid, tc.desc) {
xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
+ tc.IncMsgCnt()
tc.rmrConChan <- msg
} else {
xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
xappCtrl := &testingXappControl{}
- xappCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
+ xappCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
xappCtrl.testingMessageChannel = initTestingMessageChannel()
xappCtrl.xid_seq = 1
return xappCtrl
//
//-----------------------------------------------------------------------------
type testingE2termControl struct {
- testingRmrControl
+ testingRmrStubControl
testingMessageChannel
}
func createNewE2termControl(desc string, rtfile string, port string, stat string) *testingE2termControl {
e2termCtrl := &testingE2termControl{}
- e2termCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
+ e2termCtrl.testingRmrStubControl = initTestingRmrControl(desc, rtfile, port, stat, e2termCtrl)
e2termCtrl.testingMessageChannel = initTestingMessageChannel()
return e2termCtrl
}
//
//-----------------------------------------------------------------------------
type testingMainControl struct {
- testingControl
+ testingRmrControl
c *Control
}
func createNewMainControl(desc string, rtfile string, port string) *testingMainControl {
mainCtrl = &testingMainControl{}
- mainCtrl.testingControl = initTestingControl(desc, rtfile, port)
+ mainCtrl.testingRmrControl = initTestingControl(desc, rtfile, port)
mainCtrl.c = NewControl()
xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
go xapp.RunWithParams(mainCtrl.c, false)
var xappConn2 *testingXappControl
var e2termConn *testingE2termControl
var mainCtrl *testingMainControl
+var rtmgrHttp *testingHttpRtmgrControl
func TestMain(m *testing.M) {
xapp.Logger.Info("TestMain start")
//---------------------------------
//
//---------------------------------
- http_handler := func(w http.ResponseWriter, r *http.Request) {
- var req rtmgr_models.XappSubscriptionData
- err := json.NewDecoder(r.Body).Decode(&req)
- if err != nil {
- xapp.Logger.Error("%s", err.Error())
- }
- xapp.Logger.Info("(http handler) handling Address=%s Port=%d SubscriptionID=%d", *req.Address, *req.Port, *req.SubscriptionID)
-
- w.WriteHeader(200)
- }
-
- go func() {
- http.HandleFunc("/", http_handler)
- http.ListenAndServe("localhost:8989", nil)
- }()
+ rtmgrHttp = initTestingHttpRtmgrControl("RTMGRSTUB", "8989")
+ go rtmgrHttp.run()
//---------------------------------
//