// 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, channel string, message string, responseChannel chan 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) errorChannel := make(chan []byte) go pubnubInstance.Unsubscribe(channel, returnUnsubscribeChannel, errorChannel) go ParseUnsubscribeResponse(returnUnsubscribeChannel, channel, "unsubscribed", responseChannel) go ParseResponseDummy(errorChannel) break } /*else if (strings.Contains(response, messageAbort)){ responseChannel <- "Test unsubscribed: failed." break } else { responseChannel <- "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, responseChannel chan string) { message := "Test message" returnChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.Publish(channel, message, returnChannel, errorChannel) go ParseSubscribeResponse(pubnubInstance, returnChannel, t, channel, "", testName, cipherKey, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) }
// ParseResponse parses the publish response from the pubnub api on the returnChannel and // when the sent response is received, calls the history method of the pubnubMessaging // package to fetch 1 message. func ParseResponse(returnChannel chan []byte, pubnubInstance *pubnubMessaging.Pubnub, channel string, message string, testName string, numberOfMessages int, responseChannel chan string) { for { value, ok := <-returnChannel if !ok { break } if string(value) != "[]" { returnHistoryChannel := make(chan []byte) var errorChannel = make(chan []byte) go pubnubInstance.History(channel, 1, 0, 0, false, returnHistoryChannel, errorChannel) go ParseHistoryResponse(returnHistoryChannel, channel, message, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) break } } }
// PublishMessages calls the publish method of pubnubMessaging package numberOfMessages times // and appends the count with the message to distinguish from the others. // // Parameters: // pubnubInstance: a reference of *pubnubMessaging.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 *pubnubMessaging.Pubnub, channel string, t *testing.T, startMessagesFrom int, numberOfMessages int, message string) bool { messagesReceived := 0 messageToSend := "" for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ { messageToSend = message + strconv.Itoa(i) returnPublishChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.Publish(channel, messageToSend, returnPublishChannel, errorChannel) messagesReceived++ time.Sleep(500 * time.Millisecond) } if messagesReceived == numberOfMessages { return true } return false }
// GetServerTime calls the GetTime method of the pubnubMessaging, parses the response to get the // value and return it. func GetServerTime(pubnubInstance *pubnubMessaging.Pubnub, t *testing.T, testName string) int64 { returnTimeChannel := make(chan []byte) errorChannel := make(chan []byte) go pubnubInstance.GetTime(returnTimeChannel, errorChannel) return ParseServerTimeResponse(returnTimeChannel, t, testName) }
// ParseSubscribeResponseForPresence will look for the connection status in the response // received on the go channel. func ParseSubscribeResponseForPresence(pubnubInstance *pubnubMessaging.Pubnub, customUuid string, returnChannel chan []byte, channel string, testName string, responseChannel chan string) { for { value, ok := <-returnChannel if !ok { break } //response := fmt.Sprintf("%s", value) if string(value) != "[]" { if (testName == "CustomUuid") || (testName == "HereNow") || (testName == "HereNowWithCipher") { response := fmt.Sprintf("%s", value) message := "'" + channel + "' connected" messageReconn := "'" + channel + "' reconnected" if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) { errorChannel := make(chan []byte) returnChannel := make(chan []byte) time.Sleep(3 * time.Second) go pubnubInstance.HereNow(channel, returnChannel, errorChannel) go ParseHereNowResponse(returnChannel, channel, customUuid, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) break } } else { response := fmt.Sprintf("%s", value) message := "'" + channel + "' connected" messageReconn := "'" + channel + "' reconnected" //fmt.Println("Test3 '" + testName + "':" +response) if (strings.Contains(response, message)) || (strings.Contains(response, messageReconn)) { errorChannel2 := make(chan []byte) returnSubscribeChannel := make(chan []byte) time.Sleep(1 * time.Second) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel2) go ParseResponseDummy(returnSubscribeChannel) go ParseResponseDummy(errorChannel2) } else { if testName == "Presence" { data, _, returnedChannel, err2 := pubnubMessaging.ParseJson(value, "") var occupants []struct { Action string Uuid string Timestamp float64 Occupancy int } if err2 != nil { responseChannel <- "Test '" + testName + "': failed. Message: 1 :" + err2.Error() break } //fmt.Println("Test3 '" + testName + "':" +data) err := json.Unmarshal([]byte(data), &occupants) if err != nil { //fmt.Println("err '" + testName + "':",err) responseChannel <- "Test '" + testName + "': failed. Message: 2 :" + err.Error() break } else { channelSubRepsonseReceived := false for i := 0; i < len(occupants); i++ { if (occupants[i].Action == "join") && occupants[i].Uuid == customUuid { channelSubRepsonseReceived = true break } } if !channelSubRepsonseReceived { responseChannel <- "Test '" + testName + "': failed. Message: err3" break } if channel == returnedChannel { responseChannel <- "Test '" + testName + "': passed." break } else { responseChannel <- "Test '" + testName + "': failed. Message: err4" break } } } } } } } }
// ParseSubscribeMultiplexedResponse publishes 2 messages on 2 different channels and // when both the channels are connected // it reads the responseChannel for the 2 messages. If we get the same 2 messages as response // the test is passed. // // parameters: // pubnubInstance: an instace of *pubnubMessaging.Pubnub, // returnSubscribeChannel: the channel to read the subscribe response on. // message1: first message to publish. // message2: second message to publish. // pubnubChannel1: pubnub Channel 1 to publish the first message. // pubnubChannel2: pubnub Channel 2 to publish the second message. // testName: test name. // responseChannel: the channelto send a response back. func ParseSubscribeMultiplexedResponse(pubnubInstance *pubnubMessaging.Pubnub, returnSubscribeChannel chan []byte, message1 string, message2 string, pubnubChannel1 string, pubnubChannel2 string, testName string, responseChannel chan string) { messageCount := 0 channelCount := 0 for { value, ok := <-returnSubscribeChannel if !ok { break } if string(value) != "[]" { response := fmt.Sprintf("%s", value) message := "' connected" messageT1 := "'" + pubnubChannel1 + "' connected" messageT2 := "'" + pubnubChannel2 + "' connected" if strings.Contains(response, message) { if strings.Contains(response, messageT1) { channelCount++ } if strings.Contains(response, messageT2) { channelCount++ } if channelCount >= 2 { returnPublishChannel := make(chan []byte) errorChannelPub := make(chan []byte) go pubnubInstance.Publish(pubnubChannel1, message1, returnPublishChannel, errorChannelPub) go ParseResponseDummy(returnPublishChannel) go ParseResponseDummy(errorChannelPub) returnPublishChannel2 := make(chan []byte) errorChannelPub2 := make(chan []byte) go pubnubInstance.Publish(pubnubChannel2, message2, returnPublishChannel2, errorChannelPub2) go ParseResponseDummy(returnPublishChannel2) go ParseResponseDummy(errorChannelPub2) } } else { var s []interface{} err := json.Unmarshal(value, &s) if err == nil { if len(s) > 2 { if message, ok := s[0].([]interface{}); ok { if messageT, ok2 := message[0].(string); ok2 { if (len(message) > 0) && (messageT == message1) && (s[2].(string) == pubnubChannel1) { messageCount++ } if (len(message) > 0) && (messageT == message2) && (s[2].(string) == pubnubChannel2) { messageCount++ } } } } } if messageCount >= 2 { responseChannel <- "Test '" + testName + "': passed." break } } } } }
// ParseSubscribeResponse reads the response from the go channel and unmarshal's it. // It is used by multiple test cases and acts according to the testcase names. // The idea is to parse each message in the response based on the type of message // and test against the sent message. If both match the test case is successful. // _publishSuccessMessage is defined in the common.go file. func ParseSubscribeResponse(pubnubInstance *pubnubMessaging.Pubnub, returnChannel chan []byte, t *testing.T, channel string, message string, testName string, cipherKey string, responseChannel chan string) { for { value, ok := <-returnChannel if !ok { break } if string(value) != "[]" { response := fmt.Sprintf("%s", value) //fmt.Println("Response1:", response) if (testName == "SubscriptionConnectedForComplex") || (testName == "SubscriptionConnectedForComplexWithCipher") { message = "'" + channel + "' connected" if strings.Contains(response, message) { PublishComplexMessage(pubnubInstance, t, channel, _publishSuccessMessage, cipherKey, responseChannel) } else { //fmt.Println("resp:", response) if ParseSubscribeData(t, value, testName, cipherKey, responseChannel) { responseChannel <- "Test '" + testName + "': passed." } else { responseChannel <- "Test '" + testName + "': failed." } break } } else if (testName == "SubscriptionConnectedForSimple") || (testName == "SubscriptionConnectedForSimpleWithCipher") { message = "'" + channel + "' connected" if strings.Contains(response, message) { PublishSimpleMessage(pubnubInstance, t, channel, _publishSuccessMessage, cipherKey, responseChannel) } else { if ParseSubscribeData(t, value, testName, cipherKey, responseChannel) { responseChannel <- "Test '" + testName + "': passed." } else { responseChannel <- "Test '" + testName + "': failed." } break } } else if testName == "SubscriptionAlreadySubscribed" { message = "'" + channel + "' connected" if strings.Contains(response, message) { returnSubscribeChannel2 := make(chan []byte) errorChannel2 := make(chan []byte) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel2, false, errorChannel2) go ParseSubscribeResponse(pubnubInstance, errorChannel2, t, channel, "already subscribed", "SubscriptionAlreadySubscribedResponse", "", responseChannel) go ParseResponseDummy(returnSubscribeChannel2) } break } else if testName == "SubscriptionAlreadySubscribedResponse" { message = "'" + channel + "' already subscribed" if strings.Contains(response, message) { responseChannel <- "Test '" + testName + "': passed." } else { responseChannel <- "Test '" + testName + "': failed." //t.Error("Test '" + testName + "': failed."); } break } else if testName == "SubscriptionConnectStatus" { message = "'" + channel + "' connected" if strings.Contains(response, message) { responseChannel <- "Test '" + testName + "': passed." } else { responseChannel <- "Test '" + testName + "': failed." //t.Error("Test '" + testName + "': failed."); } break } } } }