Skip to content

Commit

Permalink
Tests channel isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
pote committed Jun 30, 2016
1 parent dc129db commit 253f94c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 53 deletions.
14 changes: 14 additions & 0 deletions access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ package main

import (
"testing"

"code.google.com/p/go-uuid/uuid"
)

func createTestAccessKey() (*AccessKey, error) {
ak := &AccessKey{
Read: []string{"test-channel"},
Write: []string{},
Token: uuid.New(),
}

err := ak.Save()
return ak, err
}


func TestLoadKey(t *testing.T) {
ak, _ := createTestAccessKey()
defer ak.Delete()
Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ func main() {
log.Printf("[Main] Port: %v\n", port)
log.Printf("[Main] Cores: %v\n", runtime.NumCPU())

err := http.ListenAndServe(":" + port, websocket.Handler(ServeWebSocket)); if err != nil {
log.Println(err)
}
done := make(chan bool)
RunServer(done, port)
}

func RunServer(done chan bool, port string) {
go func() {
err := http.ListenAndServe(":" + port, websocket.Handler(ServeWebSocket)); if err != nil {
log.Println(err)
}
}()

<- done
log.Println("[Main] Stop signal detected, shutting down.")
}

func ServeWebSocket(ws *websocket.Conn) {
Expand Down
42 changes: 0 additions & 42 deletions main_test.go

This file was deleted.

74 changes: 66 additions & 8 deletions socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
"golang.org/x/net/websocket"
)

func createMatchingAccessKeys() (*AccessKey, *AccessKey) {
func TestBasicPubSub (t *testing.T) {
done := make(chan bool)
go RunServer(done, os.Getenv("PORT"))
defer func() { done <- true }()

ak1 := &AccessKey{
Read: []string{"test-channel"},
Write: []string{"test-channel"},
Expand All @@ -26,13 +30,6 @@ func createMatchingAccessKeys() (*AccessKey, *AccessKey) {

ak2.Save()

return ak1, ak2
}

func TestBasicPubSub (t *testing.T) {
ak1, ak2 := createMatchingAccessKeys()
go main(); time.Sleep(time.Second) // Give it a second, will you?

url1 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak1.Token)
url2 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak2.Token)

Expand Down Expand Up @@ -85,3 +82,64 @@ func TestBasicPubSub (t *testing.T) {
t.Error("timout reached, no message received")
}
}

func TestChannelIsolation (t *testing.T) {
done := make(chan bool)
go RunServer(done, os.Getenv("PORT"))
defer func() { done <- true }()

ak1 := &AccessKey{
Read: []string{},
Write: []string{"another-test-channel"},
Token: "blah",
}
ak1.Save()

ak2 := &AccessKey{
Read: []string{"test-channel"},
Write: []string{},
Token: "bleh",
}
ak2.Save()

url1 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak1.Token)
url2 := fmt.Sprintf("ws://localhost:%v/%v", os.Getenv("PORT"), ak2.Token)

ws1, err := websocket.Dial(url1, "", "http://localhost"); if err != nil {
t.Fatal(err)
}

ws2, err := websocket.Dial(url2, "", "http://localhost"); if err != nil {
t.Fatal(err)
}

channel := make(chan Message, 1)

go func() {
receivedMsg := Message{}
err := websocket.JSON.Receive(ws2, &receivedMsg); if err != nil {
t.Fatal(err)
}

channel <- receivedMsg
}()


originalMessage := &Message{
Channel: "another-test-channel",
Data: "hey there!",
Event: "message",
}

go func() {
time.Sleep(time.Second * 1)
websocket.JSON.Send(ws1, &originalMessage)
}()

select {
case <- channel:
t.Error("this message should not have been received")
case <- time.After(time.Second * 3):
// NoOp, all is well.
}
}

0 comments on commit 253f94c

Please sign in to comment.