Beispiel #1
0
func NewVCRBoth(name string, skipFields []string) (
	func(), func(int)) {

	vcrMu.Lock()

	skipFields = append(skipFields, defaultFieldsToSkip...)
	s, _ := recorder.New(fmt.Sprintf("%s_%s", name, "Subscribe"))
	s.UseMatcher(utils.NewPubnubSubscribeMatcher(skipFields))

	ns, _ := recorder.New(fmt.Sprintf("%s_%s", name, "NonSubscribe"))
	ns.UseMatcher(utils.NewPubnubMatcher(skipFields))

	sDial := genVcrDial()

	s.Transport.Dial = sDial

	messaging.SetSubscribeTransport(s.Transport)
	messaging.SetNonSubscribeTransport(ns.Transport)

	return func() {
			s.Stop()
			ns.Stop()

			messaging.SetSubscribeTransport(nil)
			messaging.SetNonSubscribeTransport(nil)
			vcrMu.Unlock()
		}, func(seconds int) {
			mode := recorder.ModeRecording
			if ns.Mode() == mode && s.Mode() == mode {
				time.Sleep(time.Duration(seconds) * time.Second)
			} else {
				// do not sleep
			}
		}
}
Beispiel #2
0
func NewHangingTransport() func() {
	vcrMu.Lock()
	messaging.SetNonSubscribeTransport(hangingTransport)

	return func() {
		messaging.SetNonSubscribeTransport(nil)
		vcrMu.Unlock()
	}
}
Beispiel #3
0
func NewBadJSONTransport() func() {
	vcrMu.Lock()
	messaging.SetNonSubscribeTransport(badJSONTransport)

	return func() {
		messaging.SetNonSubscribeTransport(nil)
		vcrMu.Unlock()
	}
}
Beispiel #4
0
func NewAbortedTransport() func() {
	vcrMu.Lock()
	messaging.SetNonSubscribeTransport(abortedTransport)

	return func() {
		messaging.SetNonSubscribeTransport(nil)
		vcrMu.Unlock()
	}
}
Beispiel #5
0
func NewVCRNonSubscribe(name string, skipFields []string) (
	func(), func(int)) {

	vcrMu.Lock()
	skipFields = append(skipFields, defaultFieldsToSkip...)
	ns, _ := recorder.New(fmt.Sprintf("%s_%s", name, "NonSubscribe"))
	nsMatcher := utils.NewPubnubMatcher(skipFields)
	ns.UseMatcher(nsMatcher)
	messaging.SetNonSubscribeTransport(ns.Transport)

	return func() {
			ns.Stop()

			messaging.SetNonSubscribeTransport(nil)
			vcrMu.Unlock()
		}, func(seconds int) {
			if ns.Mode() == recorder.ModeRecording {
				time.Sleep(time.Duration(seconds) * time.Second)
			} else {
				// do not sleep
			}
		}
}
Beispiel #6
0
func TestErrorNetworkUnsubscribe(t *testing.T) {
	assert := assert.New(t)

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

	channel := "Channel_UnsubscribeNetError"
	pubnubInstance := messaging.NewPubnub(PubKey, SubKey, SecKey, "", false, "")
	pubnubInstance.SetNonSubscribeTransport(abortedTransport)

	subscribeSuccess := make(chan []byte)
	subscribeError := make(chan []byte)

	go pubnubInstance.Subscribe(channel, "", subscribeSuccess, false, subscribeError)
	ExpectConnectedEvent(t, channel, "", subscribeSuccess, subscribeError)

	successGet := make(chan []byte)
	errorGet := make(chan []byte)

	go pubnubInstance.Unsubscribe(channel, successGet, errorGet)
	select {
	case value := <-successGet:
		assert.Contains(string(value), "unsubscribed")
		assert.Contains(string(value), channel)
	case err := <-errorGet:
		assert.Fail("Error response while expecting success", string(err))
	case <-timeouts(5):
		assert.Fail("WhereNow timeout 5s")
	}
	select {
	case value := <-successGet:
		assert.Fail("Success response while expecting error", string(value))
	case err := <-errorGet:
		assert.Contains(string(err), abortedTransport.PnMessage)
		assert.Contains(string(err), channel)
	case <-timeouts(5):
		assert.Fail("WhereNow timeout 5s")
	}

	messaging.SetNonSubscribeTransport(nil)
}