// ParseSubscribeResponseAndCallUnsubscribe will parse the response on the go channel.
// It will check the subscribe connection status and when connected
// it will initiate the unsubscribe request.
func ParseSubscribeResponseAndCallUnsubscribe(pubnubInstance *pubnubMessaging.Pubnub, returnChannel chan []byte, t *testing.T, channel string, message string) {
	for {
		value, ok := <-returnChannel
		if !ok {
			break
		}
		if string(value) != "[]" {
			response := fmt.Sprintf("%s", value)

			message = "'" + channel + "' " + message
			messageAbort := "'" + channel + "' aborted"
			if strings.Contains(response, message) {
				returnUnsubscribeChannel := make(chan []byte)
				go pubnubInstance.Unsubscribe(channel, returnUnsubscribeChannel)
				ParseUnsubscribeResponse(returnUnsubscribeChannel, t, channel, "unsubscribed")
				break
			} else if strings.Contains(response, messageAbort) {
				t.Error("Test unsubscribed: failed.")
				break
			} else {
				t.Error("Test unsubscribed: failed.")
				break
			}
		}
	}
}
// PublishSimpleMessage publises a message on a pubnub channel and
// calls the parse method to validate the message subscription.
func PublishSimpleMessage(pubnubInstance *pubnubMessaging.Pubnub, t *testing.T, channel string, testName string, cipherKey string) {
	message := "Test message"

	returnChannel := make(chan []byte)
	go pubnubInstance.Publish(channel, message, returnChannel)

	ParseSubscribeResponse(pubnubInstance, returnChannel, t, channel, "", testName, cipherKey)
}
// PublishComplexMessage publises a complex message on a pubnub channel and
// calls the parse method to validate the message subscription.
// CustomComplexMessage and InitComplexMessage are defined in the common.go file.
func PublishComplexMessage(pubnubInstance *pubnubMessaging.Pubnub, t *testing.T, channel string, testName string, cipherKey string) {
	customComplexMessage := InitComplexMessage()

	returnChannel := make(chan []byte)
	go pubnubInstance.Publish(channel, customComplexMessage, returnChannel)

	ParseSubscribeResponse(pubnubInstance, returnChannel, t, channel, "", testName, cipherKey)
}
// SubscribeRoutine presence notifications to a pubnub channel and waits for the response.
// Used as a go routine.
func SubscribeToPresence(channel string, pubnubInstance *pubnubMessaging.Pubnub, t *testing.T, testName string, customUuid string, returnPresenceChannel chan []byte) {
	go pubnubInstance.Subscribe(channel, returnPresenceChannel, true)
	ParsePresenceResponse(pubnubInstance, t, returnPresenceChannel, channel, testName, customUuid, false)
}
// SubscribeRoutine subscribes to a pubnub channel and waits for the response.
// Used as a go routine.
func SubscribeRoutine(channel string, pubnubInstance *pubnubMessaging.Pubnub) {
	var subscribeChannel = make(chan []byte)
	go pubnubInstance.Subscribe(channel, subscribeChannel, false)
	ParseSubscribeResponseForPresence(subscribeChannel, channel)
}