func TestSetUserStateGlobalHereNow(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/presence/setUserStateGlobalHereNow", []string{"uuid"}) defer stop() channel := "Channel_SetUserStateGlobalHereNow" uuid := "UUID_SetUserStateGlobalHereNow" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, uuid) key := "testkey" val := "testval" successChannel := make(chan []byte) errorChannel := make(chan []byte) successSet := make(chan []byte) errorSet := make(chan []byte) successGet := make(chan []byte) errorGet := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel) ExpectConnectedEvent(t, channel, "", successChannel, errorChannel) go pubnubInstance.SetUserStateKeyVal(channel, key, val, successSet, errorSet) select { case value := <-successSet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\"}", key, val) assert.Contains(actual, expectedSubstring) case err := <-errorSet: assert.Fail("Failed to set state", string(err)) case <-messaging.Timeout(): assert.Fail("Set state timeout") } sleep(PresenceServerTimeoutLower) go pubnubInstance.GlobalHereNow(true, true, successGet, errorGet) select { case value := <-successGet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\"}", key, val) assert.Contains(actual, expectedSubstring) assert.Contains(actual, pubnubInstance.GetUUID()) case err := <-errorSet: assert.Fail("Failed to get state", string(err)) case <-messaging.Timeout(): assert.Fail("Get state timeout") } go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel) }
func TestSetUserStateJSON(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRNonSubscribe( "fixtures/presence/setUserStateJSON", []string{"uuid"}) defer stop() channel := "Channel_SetUserStateJSON" uuid := "UUID_SetUserStateJSON" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, uuid) key1 := "testkey" val1 := "testval" key2 := "testkey2" val2 := "testval2" successSet := make(chan []byte) errorSet := make(chan []byte) jsonString := fmt.Sprintf("{\"%s\": \"%s\",\"%s\": \"%s\"}", key1, val1, key2, val2) go pubnubInstance.SetUserStateJSON(channel, jsonString, successSet, errorSet) select { case value := <-successSet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\", \"%s\": \"%s\"}", key2, val2, key1, val1) assert.Contains(actual, expectedSubstring) case err := <-errorSet: assert.Fail("Failed to set state", string(err)) case <-messaging.Timeout(): assert.Fail("Set state timeout") } sleep(PresenceServerTimeoutLower) go pubnubInstance.SetUserStateKeyVal(channel, key2, "", successSet, errorSet) select { case value := <-successSet: actual := string(value) expectedSubstring := fmt.Sprintf("{\"%s\": \"%s\"}", key1, val1) assert.Contains(actual, expectedSubstring) case err := <-errorSet: assert.Fail("Failed to set state", string(err)) case <-messaging.Timeout(): assert.Fail("Set state timeout") } }
// TestCustomUuid subscribes to a pubnub channel using a custom uuid and then // makes a call to the herenow method of the pubnub api. The custom id should // be present in the response else the test fails. func TestCustomUuid(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/presence/customUuid", []string{"uuid"}) defer stop() uuid := "customuuid" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid) channel := "customUuid" successChannel := make(chan []byte) errorChannel := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) successGet := make(chan []byte) errorGet := make(chan []byte) go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel) ExpectConnectedEvent(t, channel, "", successChannel, errorChannel) sleep(PresenceServerTimeoutHighter) go pubnubInstance.HereNow(channel, "", true, true, successGet, errorGet) select { case value := <-successGet: assert.Contains(string(value), uuid) var occupants struct { Uuids []map[string]string Occupancy int } err := json.Unmarshal(value, &occupants) if err != nil { assert.Fail(err.Error()) } found := false for _, v := range occupants.Uuids { if v["uuid"] == uuid { found = true } } assert.True(found) case err := <-errorGet: assert.Fail("Failed to get online users", string(err)) case <-messaging.Timeout(): assert.Fail("HereNow timeout") } go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel) }
// TestWhereNow subscribes to a pubnub channel and then // makes a call to the herenow method of the pubnub api. The occupancy should // be greater than one. func TestWhereNow(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/presence/whereNow", []string{"uuid"}) defer stop() uuid := "UUID_WhereNow" channel := "Channel_WhereNow" pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, uuid) successChannel := make(chan []byte) errorChannel := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) successGet := make(chan []byte) errorGet := make(chan []byte) go pubnubInstance.Subscribe(channel, "", successChannel, false, errorChannel) ExpectConnectedEvent(t, channel, "", successChannel, errorChannel) sleep(PresenceServerTimeoutLower) go pubnubInstance.WhereNow(uuid, successGet, errorGet) select { case value := <-successGet: assert.Contains(string(value), channel) case err := <-errorGet: assert.Fail("Failed to get channel list", string(err)) case <-messaging.Timeout(): assert.Fail("WhereNow timeout") } go pubnubInstance.Unsubscribe(channel, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, channel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel) // pubnubInstance.CloseExistingConnection() }
// PublishMessages calls the publish method of messaging package numberOfMessages times // and appends the count with the message to distinguish from the others. // // Parameters: // pubnubInstance: a reference of *messaging.Pubnub, // channel: the pubnub channel to publish the messages, // t: a reference to *testing.T, // startMessagesFrom: the message identifer, // numberOfMessages: number of messages to send, // message: message to send. // // returns a bool if the publish of all messages is successful. func PublishMessages(pubnubInstance *messaging.Pubnub, channel string, t *testing.T, startMessagesFrom int, numberOfMessages int, message string, sleep func(int)) bool { assert := assert.New(t) messagesReceived := 0 messageToSend := "" tOut := messaging.GetNonSubscribeTimeout() messaging.SetNonSubscribeTimeout(30) successChannel := make(chan []byte) errorChannel := make(chan []byte) for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ { messageToSend = message + strconv.Itoa(i) go pubnubInstance.Publish(channel, messageToSend, successChannel, errorChannel) select { case <-successChannel: messagesReceived++ case err := <-errorChannel: assert.Fail("Failed to get channel list", string(err)) case <-messaging.Timeout(): assert.Fail("WhereNow timeout") } } sleep(3) if messagesReceived == numberOfMessages { return true } messaging.SetNonSubscribeTimeout(tOut) return false }
func TestGroupSubscriptionReceiveSingleMessage(t *testing.T) { assert := assert.New(t) stop, sleep := NewVCRBoth( "fixtures/groups/receiveSingleMessage", []string{"uuid"}) defer stop() group := "Group_GroupReceiveSingleMessage" channel := "Channel_GroupReceiveSingleMessage" uuid := "UUID_GroupReceiveSingleMessage" pubnub := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid) populateChannelGroup(pubnub, group, channel) defer removeChannelGroups(pubnub, []string{group}) sleep(2) subscribeSuccessChannel := make(chan []byte) subscribeErrorChannel := make(chan []byte) successChannel := make(chan []byte) errorChannel := make(chan []byte) unsubscribeSuccessChannel := make(chan []byte) unsubscribeErrorChannel := make(chan []byte) msgReceived := make(chan bool) go pubnub.ChannelGroupSubscribe(group, subscribeSuccessChannel, subscribeErrorChannel) ExpectConnectedEvent(t, "", group, subscribeSuccessChannel, subscribeErrorChannel) go func() { select { case message := <-subscribeSuccessChannel: var msg []interface{} err := json.Unmarshal(message, &msg) if err != nil { assert.Fail(err.Error()) } assert.Len(msg, 4) assert.Equal(msg[2], channel) assert.Equal(msg[3], group) msgReceived <- true case err := <-subscribeErrorChannel: assert.Fail(string(err)) case <-timeouts(3): assert.Fail("Subscription timeout") } }() go pubnub.Publish(channel, "hey", successChannel, errorChannel) select { case <-successChannel: case err := <-errorChannel: assert.Fail("Publish error", string(err)) case <-messaging.Timeout(): assert.Fail("Publish timeout") } <-msgReceived go pubnub.ChannelGroupUnsubscribe(group, unsubscribeSuccessChannel, unsubscribeErrorChannel) ExpectUnsubscribedEvent(t, "", group, unsubscribeSuccessChannel, unsubscribeErrorChannel) // pubnub.CloseExistingConnection() }