Esempio n. 1
0
func TestClientSideClose(t *testing.T) {
	var simpleHandler *SimpleHandler
	closeChan := make(chan bool)
	onNew := func(handler wsevents.EventHandler) {
		var ok bool
		simpleHandler, ok = handler.(*SimpleHandler)
		if !ok {
			t.Fatal("handler was not a *SimpleHandler")
		}

		simpleHandler.CloseChan = closeChan
	}

	serv := httptest.NewServer(wsevents.Handler(&SimpleHandler{}, onNew))

	origin := serv.URL
	url := "ws" + serv.URL[4:]
	_, err := websocket.Dial(url, "", origin)
	if err != nil {
		t.Error(err)
	}

	serv.CloseClientConnections()

	select {
	case <-closeChan:
		if simpleHandler.Connection == nil {
			t.Fatal("OnOpen was not called!")
		}
	case <-time.After(1 * time.Second):
		t.Fatal("did not get notified of close!")
	}
}
Esempio n. 2
0
func TestSimple(t *testing.T) {
	var simpleHandler *SimpleHandler
	msgChan := make(chan string)
	onNew := func(handler wsevents.EventHandler) {
		var ok bool
		simpleHandler, ok = handler.(*SimpleHandler)
		if !ok {
			t.Fatal("handler was not a *SimpleHandler")
		}

		simpleHandler.MsgChan = msgChan
	}

	serv := httptest.NewServer(wsevents.Handler(&SimpleHandler{}, onNew))
	defer serv.CloseClientConnections()
	defer serv.Close()

	origin := serv.URL
	url := "ws" + serv.URL[4:]
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		t.Error(err)
	}

	websocket.Message.Send(ws, `{"name": "testmsg", "args": ["test 123![]{}@"]}`)

	select {
	case msg := <-msgChan:
		if msg != "test 123![]{}@" {
			t.Fatal("message did not match!")
		}
		if simpleHandler.Connection == nil {
			t.Fatal("OnOpen was not called!")
		}
	case <-time.After(1 * time.Second):
		t.Fatal("did not receive message!")
	}
}