func TestGroupSubscriptionConnectedAndUnsubscribedMultiple(t *testing.T) {
	assert := assert.New(t)

	stop, sleep := NewVCRBoth(
		"fixtures/groups/conAndUnsMultiple", []string{"uuid"})
	defer stop()

	uuid := "UUID_Multiple_CAU"
	pubnub := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)
	groupsString := "Group_ConAndUnsMult_1,Group_ConAndUnsMult_2,Group_ConAndUnsMult_3"
	groups := strings.Split(groupsString, ",")

	createChannelGroups(pubnub, groups)
	defer removeChannelGroups(pubnub, groups)

	sleep(2)

	subscribeSuccessChannel := make(chan []byte)
	subscribeErrorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	errorChannel := make(chan []byte)
	await := make(chan bool)

	go pubnub.ChannelGroupSubscribe(groupsString,
		subscribeSuccessChannel, subscribeErrorChannel)

	go func() {
		var messages []string

		for {
			select {
			case message := <-subscribeSuccessChannel:
				var msg []interface{}

				err := json.Unmarshal(message, &msg)
				if err != nil {
					assert.Fail(err.Error())
				}

				assert.Contains(msg[1].(string), "Subscription to channel group")
				assert.Contains(msg[1].(string), "connected")
				assert.Len(msg, 3)

				messages = append(messages, string(msg[2].(string)))
			case err := <-subscribeErrorChannel:
				assert.Fail("Subscribe error", string(err))
			case <-timeouts(10):
				break
			}

			if len(messages) == 3 {
				break
			}
		}

		assert.True(utils.AssertStringSliceElementsEqual(groups, messages),
			fmt.Sprintf("Expected groups: %s. Actual groups: %s\n", groups, messages))

		await <- true
	}()

	select {
	case <-await:
	case <-timeouts(20):
		assert.Fail("Receive connected messages timeout")
	}

	go pubnub.ChannelGroupUnsubscribe(groupsString, successChannel, errorChannel)
	go func() {
		var messages []string
		var events int

		for {
			select {
			case message := <-successChannel:
				var msg []interface{}

				err := json.Unmarshal(message, &msg)
				if err != nil {
					var event map[string]interface{}
					err := json.Unmarshal(message, &event)
					if err != nil {
						assert.Fail(err.Error())
					}

					assert.Equal(event["action"].(string), "leave")
					assert.Equal(event["message"].(string), "OK")
					events++
				} else {

					assert.Contains(msg[1].(string), "Subscription to channel group")
					assert.Contains(msg[1].(string), "unsubscribed")
					assert.Len(msg, 3)

					messages = append(messages, string(msg[2].(string)))
				}
			case err := <-errorChannel:
				assert.Fail("Subscribe error", string(err))
			case <-timeouts(10):
				break
			}

			if len(messages) == 3 && events == 3 {
				break
			}
		}

		assert.True(utils.AssertStringSliceElementsEqual(groups, messages),
			fmt.Sprintf("Expected groups: %s. Actual groups: %s\n", groups, messages))

		await <- true
	}()

	select {
	case <-await:
	case <-timeouts(20):
		assert.Fail("Receive unsubscribed messages timeout")
	}

	// pubnub.CloseExistingConnection()
}
Esempio n. 2
0
// TestMultiSubscriptionConnectStatus send out a pubnub multi channel subscribe request and
// parses the response for multiple connection status.
func TestMultiSubscriptionConnectStatus(t *testing.T) {
	// TODO: test passes successfully, but some errors about extra interactions exists
	assert := assert.New(t)

	stop, _ := NewVCRBoth("fixtures/subscribe/connectMultipleStatus", []string{"uuid"})
	defer stop()

	channels := "Channel_ConnectStatus_14,Channel_ConnectStatus_992"
	uuid := "UUID_ConnectStatus"
	pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)

	expectedChannels := strings.Split(channels, ",")
	actualChannels := []string{}
	var actualMu sync.Mutex

	successChannel := make(chan []byte)
	errorChannel := make(chan []byte)
	unsubscribeSuccessChannel := make(chan []byte)
	unsubscribeErrorChannel := make(chan []byte)
	await := make(chan bool)

	go pubnubInstance.Subscribe(channels, "", successChannel, false, errorChannel)
	go func() {

		for {
			select {
			case resp := <-successChannel:
				var response []interface{}

				err := json.Unmarshal(resp, &response)
				if err != nil {
					assert.Fail(err.Error())
				}

				assert.Len(response, 3)
				assert.Contains(response[1].(string), "Subscription to channel")
				assert.Contains(response[1].(string), "connected")

				actualMu.Lock()
				actualChannels = append(actualChannels, response[2].(string))
				l := len(actualChannels)
				actualMu.Unlock()

				if l == 2 {
					await <- true
					return
				}

			case err := <-errorChannel:
				assert.Fail(string(err))

				await <- false
			case <-timeouts(5):
				assert.Fail("Subscribe timeout 5s")
				await <- false
			}

		}
	}()

	select {
	case <-await:
		actualMu.Lock()
		assert.True(utils.AssertStringSliceElementsEqual(expectedChannels, actualChannels),
			fmt.Sprintf("%s(expected) should be equal to %s(actual)", expectedChannels, actualChannels))
		actualMu.Unlock()
	case <-timeouts(10):
		assert.Fail("Timeout connecting channels")
	}

	go pubnubInstance.Unsubscribe(channels, unsubscribeSuccessChannel, unsubscribeErrorChannel)
	ExpectUnsubscribedEvent(t, channels, "", unsubscribeSuccessChannel, unsubscribeErrorChannel)
}
Esempio n. 3
0
func waitForEventOnEveryChannel(t *testing.T, channels, groups []string,
	cnAction, prAction string, successChannel, errorChannel <-chan []byte) {

	var triggeredChannels []string
	var triggeredGroups []string

	channel := make(chan bool)

	go func() {
		for {
			select {
			case event := <-successChannel:
				var ary []interface{}

				eventString := string(event)
				assert.Contains(t, eventString, cnAction,
					"While expecting connection messages to be equal")

				err := json.Unmarshal(event, &ary)
				if err != nil {
					assert.Fail(t, err.Error())
				}

				if strings.Contains(eventString, "channel group") {
					triggeredGroups = append(triggeredGroups, ary[2].(string))
				} else if strings.Contains(eventString, "channel") {
					triggeredChannels = append(triggeredChannels, ary[2].(string))
				}
				if utils.AssertStringSliceElementsEqual(triggeredChannels, channels) &&
					utils.AssertStringSliceElementsEqual(triggeredGroups, groups) {
					channel <- true
					return
				}
			case err := <-errorChannel:
				assert.Fail(t, fmt.Sprintf(
					"Error while expecting for a %s connection event", cnAction),
					string(err))
				channel <- false
				return
			}
		}
	}()

	select {
	case <-channel:
	case <-timeouts(20):
		assert.Fail(t, fmt.Sprintf(
			"Timeout occured for %s event. Expected channels/groups: %s/%s. "+
				"Received channels/groups: %s/%s\n",
			cnAction, channels, groups, triggeredChannels, triggeredGroups))
	}

	if "unsubscribed" == cnAction {
		go func() {
			for {
				select {
				case ev := <-successChannel:
					var event messaging.PresenceResonse

					err := json.Unmarshal(ev, &event)
					if err != nil {
						assert.Fail(t, err.Error(), string(ev))
					}

					assert.Equal(t, prAction, event.Action)
					assert.Equal(t, 200, event.Status)
					channel <- true
				case err := <-errorChannel:
					assert.Fail(t,
						fmt.Sprintf("Error while expecting for a %s presence event", prAction),
						string(err))
					channel <- false
					return
				}
			}
		}()

		select {
		case <-channel:
		case <-timeouts(20):
			assert.Fail(t, fmt.Sprintf(
				"Timeout occured for %s event. Expected channels/groups: %s/%s. "+
					"Received channels/groups: %s/%s\n",
				prAction, channels, groups, triggeredChannels, triggeredGroups))
		}
	}
}