lichao
2021-05-19 96a894834650e219cdd9b4c9756f7b1790bcd838
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bhsgo
 
import (
    "fmt"
    "testing"
    "time"
    "unsafe"
 
    bh "basic.com/valib/bhshmq.git/proto/source/bhome_msg"
)
 
func ServerCallback(src unsafe.Pointer, proc_id *string, req *bh.MsgRequestTopic) {
    fmt.Println("user server cb called, request topic: " + string(req.Topic) + ", data:" + string(req.Data))
    reply := bh.MsgRequestTopicReply{}
    reply.Data = []byte("reply 1234")
    SendReply(src, &reply)
}
 
func SubDataCallback(proc_id *string, pub *bh.MsgPublish) {
    fmt.Println("user sub data cb called")
}
func ClientCallback(proc_id *string, msg_id *[]byte, reply *bh.MsgRequestTopicReply) {
    fmt.Println("user client cb reply: " + string(reply.Data))
}
func TestRegister(t *testing.T) {
    proc_id := "test_proc"
    proc := bh.ProcInfo{}
    proc.ProcId = []byte(proc_id)
    reply := bh.MsgCommonReply{}
    defer Cleanup()
 
    StartWorker(ClientCallback, ServerCallback, SubDataCallback)
 
    r := Register(&proc, &reply, 1000)
    if r {
        fmt.Println("register ok")
    } else {
        fmt.Println("register failed")
        return
    }
    r = Unregister(&proc, &reply, 1000)
    if r {
        fmt.Println("Unregister ok")
    } else {
        fmt.Println("Unregister failed")
    }
 
    r = Register(&proc, &reply, 1000)
    if r {
        fmt.Println("register ok")
    } else {
        fmt.Println("register failed")
        t.Log("register error")
        return
    }
 
    r = HeartbeatEasy(1000)
    if r {
        fmt.Println("heartbeat ok")
    } else {
        fmt.Println("heartbeat failed")
    }
 
    topics := bh.MsgTopicList{}
    topics.TopicList = append(topics.TopicList, []byte("topic0"), []byte("topic1"))
    RegisterTopics(&topics, &reply, 0)
    if r {
        fmt.Println("reg topics ok")
    } else {
        fmt.Println("reg topics failed")
    }
    req := bh.MsgRequestTopic{}
    time.Sleep(time.Second * 1)
    req.Topic = []byte("topic0")
    req.Data = []byte("data0")
    // var msg_id []byte
    // AsyncRequest(&req, &msg_id)
    // fmt.Println(msg_id)
    // time.Sleep(time.Second * 5)
 
    pid := ""
    rr := bh.MsgRequestTopicReply{}
    dest := bh.BHAddress{}
    for i := 0; i < 10000; i++ {
        if Request(&dest, &req, &pid, &rr, 3000) {
            fmt.Println("server:" + pid + ", reply:" + string(rr.Data))
        } else {
            e, s := GetLastError()
            fmt.Println("ec:", e, ", msg:"+s)
        }
    }
}