xmpp/status_test.go
author Chris Jones <christian.jones@sri.com>
Sat, 28 Sep 2013 13:02:17 -0600
changeset 153 bbd4166df95d
permissions -rw-r--r--
Simplified the API: There's only one constructor, and it does everything necessary to initiate the stream. StartSession() and Roster.Update() have both been eliminated.

package xmpp

import (
	"testing"
	"time"
)

func TestStatusListen(t *testing.T) {
	sm := newStatmgr(nil)
	l := sm.newListener()
	stat, ok := <-l
	if !ok {
		t.Error()
	} else if stat != StatusUnconnected {
		t.Errorf("got %d", stat)
	}

	sm.setStatus(StatusConnected)
	stat, ok = <-l
	if !ok {
		t.Error()
	} else if stat != StatusConnected {
		t.Errorf("got %d", stat)
	}

	sm.setStatus(StatusBound)
	stat, ok = <-l
	if !ok {
		t.Error()
	} else if stat != StatusBound {
		t.Errorf("got %d", stat)
	}

	sm.setStatus(StatusShutdown)
	stat = <-l
	if stat != StatusShutdown {
		t.Errorf("got %d", stat)
	}
}

func TestAwaitStatus(t *testing.T) {
	sm := newStatmgr(nil)

	syncCh := make(chan int)

	go func() {
		sm.setStatus(StatusConnected)
		sm.setStatus(StatusBound)
		time.Sleep(100 * time.Millisecond)
		syncCh <- 0
	}()

	err := sm.awaitStatus(StatusBound)
	if err != nil {
		t.Fatal(err)
	}
	select {
	case <-syncCh:
		t.Fatal("didn't wait")
	default:
	}
	<-syncCh
}