func CommonUserState(pubnubInstance *messaging.Pubnub, t *testing.T, channel string, key string, val string, testName string) { returnSubscribeChannel := make(chan []byte) errorChannel := make(chan []byte) waitChannel := make(chan string) //returnChannel := make(chan []byte) responseChannel := make(chan string) go pubnubInstance.Subscribe(channel, "", returnSubscribeChannel, false, errorChannel) go ParseSubcribeResponseForUserState(pubnubInstance, t, returnSubscribeChannel, channel, key, val, testName, responseChannel) go ParseResponseDummy(errorChannel) //go ParseErrorResponse(errorChannel, responseChannel) go WaitForCompletion(responseChannel, waitChannel) ParseWaitResponse(waitChannel, t, testName) go pubnubInstance.Unsubscribe(channel, returnSubscribeChannel, errorChannel) pubnubInstance.CloseExistingConnection() time.Sleep(2 * time.Second) }
// 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 *messaging.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 } } } }
// ParseSubscribeResponseForPresence will look for the connection status in the response // received on the go channel. func ParseSubscribeResponseForPresence(pubnubInstance *messaging.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) //fmt.Println(response); 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, true, true, returnChannel, errorChannel) go ParseHereNowResponse(returnChannel, channel, customUuid, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) break } } else if testName == "WhereNow" { 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.WhereNow(customUuid, returnChannel, errorChannel) go ParseHereNowResponse(returnChannel, channel, customUuid, testName, responseChannel) go ParseErrorResponse(errorChannel, responseChannel) break } } else if testName == "GlobalHereNow" { 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.GlobalHereNow(true, false, 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 := messaging.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 } } } } } } } }