Пример #1
0
func subscribeHandler(successChannel, errorChannel chan []byte) {

	for {
		select {
		case response := <-successChannel:
			fmt.Printf("Success response: %s", response)
		case err := <-errorChannel:
			fmt.Printf("Error response: %s", err)
		case <-messaging.SubscribeTimeout():
			fmt.Printf("Subscirbe request timeout")
		}
	}
}
Пример #2
0
func TestPubnubGrantSubscribeParallelUnsafe(t *testing.T) {
	wg := sync.WaitGroup{}
	pubnub := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, "")

	for j := 0; j < numGrants; j++ {
		wg.Add(1)
		go func(j int) {
			defer wg.Done()
			callback, err := make(chan []byte), make(chan []byte)

			go pubnub.GrantSubscribe(channel, true, true, ttl, authKey, callback, err)
			select {
			case <-callback:
			case <-err:
			case <-messaging.SubscribeTimeout():
			}
		}(j)
	}
	wg.Wait()

}
func TestWildcardSubscriptionMessage(t *testing.T) {
	assert := assert.New(t)

	stop, _ := NewVCRBoth("fixtures/wildcard/message", []string{"uuid"})
	defer stop()

	uuid := "UUID_SubscribeMajor"
	major := "Channel_SubscribeMajor"
	minor := "Channel_SubscribeMinor"
	channel := fmt.Sprintf("%s.%s", major, minor)
	wildcard := fmt.Sprintf("%s.*", major)
	pubnubInstance := messaging.NewPubnub(PubKey, SubKey, "", "", false, uuid)

	subscribeSuccessChannel := make(chan []byte)
	subscribeErrorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	errorChannel := make(chan []byte)

	await := make(chan bool)

	go pubnubInstance.Subscribe(wildcard, "",
		subscribeSuccessChannel, false, subscribeErrorChannel)

	ExpectConnectedEvent(t, wildcard, "", subscribeSuccessChannel,
		subscribeErrorChannel)

	go func() {
		select {
		case message := <-subscribeSuccessChannel:
			var msg []interface{}

			err := json.Unmarshal(message, &msg)
			if err != nil {
				assert.Fail(err.Error())
			}

			assert.Contains(string(message), "hey")
			assert.Equal(channel, msg[2].(string))
			assert.Equal(wildcard, msg[3].(string))
			await <- true
		case err := <-subscribeErrorChannel:
			assert.Fail(string(err))
			await <- false
		case <-messaging.SubscribeTimeout():
			assert.Fail("Subscribe timeout")
			await <- false
		}
	}()

	go pubnubInstance.Publish(channel, "hey", successChannel, errorChannel)
	select {
	case <-successChannel:
	case err := <-errorChannel:
		assert.Fail(string(err))
	}

	<-await

	go pubnubInstance.Unsubscribe(wildcard, successChannel, errorChannel)
	ExpectUnsubscribedEvent(t, wildcard, "", successChannel, errorChannel)

	// pubnubInstance.CloseExistingConnection()
}