Пример #1
0
// 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
							}
						}
					}
				}
			}
		}
	}
}
Пример #2
0
// 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
			}
		}
	}
}