xmpp/status_test.go
changeset 153 bbd4166df95d
equal deleted inserted replaced
152:69c5b4382e39 153:bbd4166df95d
       
     1 package xmpp
       
     2 
       
     3 import (
       
     4 	"testing"
       
     5 	"time"
       
     6 )
       
     7 
       
     8 func TestStatusListen(t *testing.T) {
       
     9 	sm := newStatmgr(nil)
       
    10 	l := sm.newListener()
       
    11 	stat, ok := <-l
       
    12 	if !ok {
       
    13 		t.Error()
       
    14 	} else if stat != StatusUnconnected {
       
    15 		t.Errorf("got %d", stat)
       
    16 	}
       
    17 
       
    18 	sm.setStatus(StatusConnected)
       
    19 	stat, ok = <-l
       
    20 	if !ok {
       
    21 		t.Error()
       
    22 	} else if stat != StatusConnected {
       
    23 		t.Errorf("got %d", stat)
       
    24 	}
       
    25 
       
    26 	sm.setStatus(StatusBound)
       
    27 	stat, ok = <-l
       
    28 	if !ok {
       
    29 		t.Error()
       
    30 	} else if stat != StatusBound {
       
    31 		t.Errorf("got %d", stat)
       
    32 	}
       
    33 
       
    34 	sm.setStatus(StatusShutdown)
       
    35 	stat = <-l
       
    36 	if stat != StatusShutdown {
       
    37 		t.Errorf("got %d", stat)
       
    38 	}
       
    39 }
       
    40 
       
    41 func TestAwaitStatus(t *testing.T) {
       
    42 	sm := newStatmgr(nil)
       
    43 
       
    44 	syncCh := make(chan int)
       
    45 
       
    46 	go func() {
       
    47 		sm.setStatus(StatusConnected)
       
    48 		sm.setStatus(StatusBound)
       
    49 		time.Sleep(100 * time.Millisecond)
       
    50 		syncCh <- 0
       
    51 	}()
       
    52 
       
    53 	err := sm.awaitStatus(StatusBound)
       
    54 	if err != nil {
       
    55 		t.Fatal(err)
       
    56 	}
       
    57 	select {
       
    58 	case <-syncCh:
       
    59 		t.Fatal("didn't wait")
       
    60 	default:
       
    61 	}
       
    62 	<-syncCh
       
    63 }