github.com/emitter-io/go/v2@v2.1.0/emitter_test.go (about) 1 package emitter 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestEndToEnd(t *testing.T) { 12 clientA(t) 13 clientB(t) 14 15 // stop after 10 seconds 16 time.Sleep(1 * time.Second) 17 } 18 19 func clientA(t *testing.T) { 20 const key = "RUvY5GTEOUmIqFs_zfpJcfTqBUIKBhfs" // read on sdk-integration-test/#/ 21 22 // Create the client and connect to the broker 23 c, _ := Connect("tcp://localhost:8080", func(_ *Client, msg Message) { 24 fmt.Printf("[emitter] -> [A] received: '%s' topic: '%s'\n", msg.Payload(), msg.Topic()) 25 }) 26 27 // Subscribe to demo channel 28 fmt.Println("[emitter] <- [A] subscribing to 'demo/...'") 29 err := c.Subscribe(key, "sdk-integration-test/", nil) 30 assert.NoError(t, err) 31 } 32 33 func clientB(t *testing.T) { 34 const key = "pGrtRRL6RrjAdExSArkMzBZOoWr2eB3L" // everything on sdk-integration-test/ 35 36 // Create the client and connect to the broker 37 c, _ := Connect("tcp://localhost:8080", func(_ *Client, msg Message) { 38 fmt.Printf("[emitter] -> [B] received: '%s' topic: '%s'\n", msg.Payload(), msg.Topic()) 39 }) 40 41 c.OnPresence(func(_ *Client, ev PresenceEvent) { 42 fmt.Printf("[emitter] -> [B] presence event: %d subscriber(s) at topic: '%s'\n", len(ev.Who), ev.Channel) 43 }) 44 45 fmt.Println("[emitter] <- [B] querying own name") 46 id := c.ID() 47 assert.NotEmpty(t, id) 48 49 // Subscribe to demo channel 50 c.Subscribe(key, "sdk-integration-test/", func(_ *Client, msg Message) { 51 fmt.Printf("[emitter] -> [B] received on specific handler: '%s' topic: '%s'\n", msg.Payload(), msg.Topic()) 52 }) 53 54 // Ask for presence 55 fmt.Println("[emitter] <- [B] asking for presence on 'sdk-integration-test/'") 56 err := c.Presence(key, "sdk-integration-test/", true, false) 57 assert.NoError(t, err) 58 59 // Publish to the channel 60 fmt.Println("[emitter] <- [B] publishing to 'sdk-integration-test/'") 61 err = c.Publish(key, "sdk-integration-test/", "hello") 62 assert.NoError(t, err) 63 } 64 65 func TestFormatTopic(t *testing.T) { 66 tests := []struct { 67 key string 68 channel string 69 options []Option 70 result string 71 }{ 72 {channel: "a/b/c", result: "a/b/c/"}, 73 {key: "key", channel: "channel", result: "key/channel/"}, 74 {key: "key", channel: "a/b/c", result: "key/a/b/c/"}, 75 {key: "key", channel: "a/b/c", options: []Option{WithoutEcho()}, result: "key/a/b/c/?me=0"}, 76 {key: "key", channel: "a/b/c", options: []Option{WithoutEcho(), WithAtLeastOnce(), WithLast(100)}, result: "key/a/b/c/?me=0&last=100"}, 77 {key: "key", channel: "a/b/c", options: []Option{WithAtLeastOnce(), WithoutEcho(), WithLast(100)}, result: "key/a/b/c/?me=0&last=100"}, 78 {key: "key", channel: "a/b/c", options: []Option{WithoutEcho(), WithLast(100), WithAtLeastOnce()}, result: "key/a/b/c/?me=0&last=100"}, 79 } 80 81 for _, tc := range tests { 82 topic := formatTopic(tc.key, tc.channel, tc.options) 83 assert.Equal(t, tc.result, topic) 84 } 85 } 86 87 func TestGetHeader(t *testing.T) { 88 tests := []struct { 89 options []Option 90 qos byte 91 retain bool 92 }{ 93 94 {options: []Option{WithoutEcho()}, qos: 0, retain: false}, 95 {options: []Option{WithoutEcho(), WithAtLeastOnce(), WithLast(100)}, qos: 1, retain: false}, 96 {options: []Option{WithAtLeastOnce(), WithoutEcho(), WithLast(100)}, qos: 1, retain: false}, 97 {options: []Option{WithoutEcho(), WithLast(100), WithAtLeastOnce()}, qos: 1, retain: false}, 98 {options: []Option{WithoutEcho(), WithRetain(), WithAtMostOnce()}, qos: 0, retain: true}, 99 } 100 101 for _, tc := range tests { 102 qos, retain := getHeader(tc.options) 103 assert.Equal(t, tc.qos, qos) 104 assert.Equal(t, tc.retain, retain) 105 } 106 } 107 108 func TestFormatShare(t *testing.T) { 109 topic := formatShare("/key/", "share1", "/a/b/c/", []Option{WithoutEcho()}) 110 assert.Equal(t, "key/$share/share1/a/b/c/?me=0", topic) 111 } 112 113 func TestPresence(t *testing.T) { 114 c := NewClient() 115 116 var events []PresenceEvent 117 c.OnPresence(func(_ *Client, ev PresenceEvent) { 118 events = append(events, ev) 119 }) 120 121 c.onMessage(nil, &message{ 122 topic: "emitter/presence/", 123 payload: ` {"time":1589626821,"event":"status","channel":"retain-demo/","who":[{"id":"B"}, {"id":"C"}]}`, 124 }) 125 126 c.onMessage(nil, &message{ 127 topic: "emitter/presence/", 128 payload: ` {"time":1589626821,"event":"subscribe","channel":"retain-demo/","who":{"id":"A"}}`, 129 }) 130 131 assert.Equal(t, 2, len(events)) 132 } 133 134 /* 135 func TestHistory(t *testing.T) { 136 c, err := Connect("tcp://localhost:8080", nil) 137 assert.NoError(t, err) 138 139 for messageHistory, err := range c.History("JN8kaVOZQtG-G6QHnbFzcI-uyS_M3L5q", "test/", 1685608812, 1757293928, 5) { 140 if err != nil { 141 fmt.Println(err) 142 break 143 } 144 fmt.Println(messageHistory) 145 if messageHistory.Payload == "Hello World3" { 146 break 147 } 148 } 149 150 }*/