Esempio n. 1
0
// ResumeOnReconnect contains the actual impementation of both TestResumeOnReconnectFalse and TestResumeOnReconnectTrue
// the parameter b determines of resume on reconnect setting is true or false.
//
// The test contains a data race
func ResumeOnReconnect(t *testing.T, b bool) {
	testName := "ResumeOnReconnectFalse"
	if b {
		messaging.SetResumeOnReconnect(true)
		testName = "ResumeOnReconnectTrue"
	} else {
		messaging.SetResumeOnReconnect(false)
	}
	r := GenRandom()
	pubnubChannel := fmt.Sprintf("testChannel_subror_%d", r.Intn(20))

	pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")
	returnSubscribeChannel := make(chan []byte)
	errorChannelSub := make(chan []byte)
	responseChannelSub := make(chan string)
	waitChannelSub := make(chan string)

	messaging.SetSubscribeTimeout(12)
	go pubnubInstance.Subscribe(pubnubChannel, "", returnSubscribeChannel, false, errorChannelSub)
	go ParseSubscribeForTimetoken(pubnubInstance, pubnubChannel, returnSubscribeChannel, errorChannelSub, testName, responseChannelSub)
	go WaitForCompletion(responseChannelSub, waitChannelSub)
	ParseWaitResponse(waitChannelSub, t, testName)
	messaging.SetSubscribeTimeout(310)
	go pubnubInstance.Unsubscribe(pubnubChannel, returnSubscribeChannel, errorChannelSub)
	pubnubInstance.CloseExistingConnection()
}
// TestResumeOnReconnectTrue upon reconnect, it should use the last successfully retrieved timetoken.
// This has the effect of continuing, or “catching up” to missed traffic.
func TestResumeOnReconnectTrue(t *testing.T) {
	messaging.SetResumeOnReconnect(true)

	r := GenRandom()
	assert := assert.New(t)
	pubnubChannel := fmt.Sprintf("testChannel_subror_%d", r.Intn(20))
	pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, "")

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

	messaging.SetSubscribeTimeout(3)

	go pubnubInstance.Subscribe(pubnubChannel, "", successChannel, false, errorChannel)
	for {
		select {
		case <-successChannel:
		case value := <-errorChannel:
			if string(value) != "[]" {
				newPubnubTest := &messaging.PubnubUnitTest{}

				assert.Equal(newPubnubTest.GetTimeToken(pubnubInstance), newPubnubTest.GetSentTimeToken(pubnubInstance))
			}
			return
		case <-messaging.Timeouts(60):
			assert.Fail("Subscribe timeout")
			return
		}
	}

	messaging.SetSubscribeTimeout(310)

	go pubnubInstance.Unsubscribe(pubnubChannel, unsubscribeSuccessChannel, unsubscribeErrorChannel)
	ExpectUnsubscribedEvent(t, pubnubChannel, "", unsubscribeSuccessChannel, unsubscribeErrorChannel)

	pubnubInstance.CloseExistingConnection()
}
Esempio n. 3
0
func NewAccessGrant(ao *AccessGrantOptions, cs *ClientSettings) *AccessGrant {
	messaging.SetResumeOnReconnect(ao.ResumeOnReconnect)
	messaging.SetSubscribeTimeout(uint16(ao.SubscribeTimeout))
	messaging.SetOrigin(ao.Origin)

	p := sync.Pool{
		New: func() interface{} {
			return NewPubNubClient(cs)
		},
	}

	return &AccessGrant{pool: p}
}
Esempio n. 4
0
// Init asks the user the basic settings to initialize to the pubnub struct.
// Settings include the pubnub channel(s) to connect to.
// Ssl settings
// Cipher key
// Secret Key
// Custom Uuid
// Proxy details
//
// The method returns false if the channel name is not provided.
//
// returns: a bool, true if the user completed the initail settings.
func Init() (b bool) {
	fmt.Println("")
	fmt.Println(messaging.VersionInfo())
	messaging.SetMaxIdleConnsPerHost(20)
	fmt.Println("")
	fmt.Println("Please enter the channel name(s). Enter multiple channels separated by comma without spaces.")
	reader := bufio.NewReader(os.Stdin)

	line, _, err := reader.ReadLine()
	if err != nil {
		fmt.Println(err)
	} else {
		connectChannels = string(line)
		if strings.TrimSpace(connectChannels) != "" {
			fmt.Println("Channel: ", connectChannels)
			fmt.Println("Enable SSL? Enter y for Yes, n for No.")
			var enableSsl string
			fmt.Scanln(&enableSsl)

			if enableSsl == "y" || enableSsl == "Y" {
				ssl = true
				fmt.Println("SSL enabled")
			} else {
				ssl = false
				fmt.Println("SSL disabled")
			}

			fmt.Println("Please enter a subscribe key, leave blank for default key.")
			fmt.Scanln(&subscribeKey)

			if strings.TrimSpace(subscribeKey) == "" {
				subscribeKey = "demo"
			}
			fmt.Println("Subscribe Key: ", subscribeKey)
			fmt.Println("")

			fmt.Println("Please enter a publish key, leave blank for default key.")
			fmt.Scanln(&publishKey)
			if strings.TrimSpace(publishKey) == "" {
				publishKey = "demo"
			}
			fmt.Println("Publish Key: ", publishKey)
			fmt.Println("")

			fmt.Println("Please enter a secret key, leave blank for default key.")
			fmt.Scanln(&secretKey)
			if strings.TrimSpace(secretKey) == "" {
				secretKey = "demo"
			}
			fmt.Println("Secret Key: ", secretKey)
			fmt.Println("")

			fmt.Println("Please enter a CIPHER key, leave blank if you don't want to use this.")
			fmt.Scanln(&cipher)
			fmt.Println("Cipher: ", cipher)

			fmt.Println("Please enter a Custom UUID, leave blank for default.")
			fmt.Scanln(&uuid)
			fmt.Println("UUID: ", uuid)

			fmt.Println("Display error messages? Enter y for Yes, n for No. Default is Yes")
			var enableErrorMessages = "y"
			fmt.Scanln(&enableErrorMessages)

			if enableErrorMessages == "y" || enableErrorMessages == "Y" {
				displayError = true
				fmt.Println("Error messages will be displayed")
			} else {
				displayError = false
				fmt.Println("Error messages will not be displayed")
			}

			fmt.Println("Enable resume on reconnect? Enter y for Yes, n for No. Default is Yes")
			var enableResumeOnReconnect = "y"
			fmt.Scanln(&enableResumeOnReconnect)

			if enableResumeOnReconnect == "y" || enableResumeOnReconnect == "Y" {
				messaging.SetResumeOnReconnect(true)
				fmt.Println("Resume on reconnect enabled")
			} else {
				messaging.SetResumeOnReconnect(false)
				fmt.Println("Resume on reconnect disabled")
			}

			fmt.Println("Set subscribe timeout? Enter numerals.")
			var subscribeTimeout = ""
			fmt.Scanln(&subscribeTimeout)
			val, err := strconv.Atoi(subscribeTimeout)
			if err != nil {
				fmt.Println("Entered value is invalid. Using default value.")
			} else {
				messaging.SetSubscribeTimeout(uint16(val))
			}

			fmt.Println("Enable logging? Enter y for Yes, n for No. Default is Yes")
			var enableLogging = "y"
			fmt.Scanln(&enableLogging)

			if enableLogging == "y" || enableLogging == "Y" {
				messaging.LoggingEnabled(true)
				logfileName := "pubnubMessaging.log"
				f, err := os.OpenFile(logfileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
				if err != nil {
					fmt.Println("error opening file: ", err.Error())
					fmt.Println("Logging disabled")
				} else {
					fmt.Println("Logging enabled writing to ", logfileName)
					messaging.SetLogOutput(f)
				}
			} else {
				messaging.SetResumeOnReconnect(false)
				fmt.Println("Logging disabled")
			}

			messaging.SetOrigin("pubsub.pubnub.com")

			var pubInstance = messaging.NewPubnub(publishKey, subscribeKey, secretKey, cipher, ssl, uuid)
			pub = pubInstance

			SetupProxy()

			presenceHeartbeat := askNumber16("Presence Heartbeat", true)
			pub.SetPresenceHeartbeat(presenceHeartbeat)
			fmt.Println(fmt.Sprintf("Presence Heartbeat set to :%d", pub.GetPresenceHeartbeat()))

			presenceHeartbeatInterval := askNumber16("Presence Heartbeat Interval", true)
			pub.SetPresenceHeartbeat(presenceHeartbeatInterval)
			fmt.Println(fmt.Sprintf("Presence Heartbeat set to :%d", pub.GetPresenceHeartbeat()))

			fmt.Println("Pubnub instance initialized")

			return true
		}
		fmt.Println("Channel cannot be empty.")
	}
	return false
}