Skip to content

Commit

Permalink
Adds socket testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pote committed Jun 30, 2016
1 parent 4bc34b6 commit dc129db
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
1 change: 1 addition & 0 deletions access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"

"github.com/garyburd/redigo/redis"
)

Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

import (
"github.com/garyburd/redigo/redis"
"github.com/pote/redisurl"
"golang.org/x/net/websocket"
"log"
"lua"
"net/http"
"os"
"runtime"
"strconv"
"strings"

"github.com/garyburd/redigo/redis"
"github.com/pote/redisurl"
"golang.org/x/net/websocket"

"lua"
)

var RedisPool *redis.Pool = SetupRedis()
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"code.google.com/p/go-uuid/uuid"
"fmt"
"golang.org/x/net/websocket"
"os"
"testing"
"time"

"golang.org/x/net/websocket"
"code.google.com/p/go-uuid/uuid"
)

func createTestAccessKey() (*AccessKey, error) {
Expand Down
5 changes: 3 additions & 2 deletions socket.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"code.google.com/p/go-uuid/uuid"
"encoding/json"
"log"

"code.google.com/p/go-uuid/uuid"
"github.com/garyburd/redigo/redis"
"golang.org/x/net/websocket"
"log"
)

type Socket struct {
Expand Down
87 changes: 87 additions & 0 deletions socket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"fmt"
"os"
"testing"
"time"

"golang.org/x/net/websocket"
)

func createMatchingAccessKeys() (*AccessKey, *AccessKey) {
ak1 := &AccessKey{
Read: []string{"test-channel"},
Write: []string{"test-channel"},
Token: "blah",
}

ak1.Save()

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

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)

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: "test-channel",
Data: "hey there!",
Event: "message",
}

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

select {
case receivedMessage := <- channel:
if originalMessage.Channel != receivedMessage.Channel {
t.Error("Received message channel is incorrect")
}

if originalMessage.Data != receivedMessage.Data {
t.Error("Received message Data is incorrect")
}

if originalMessage.Event != receivedMessage.Event {
t.Error("Received message Event is incorrect")
}

case <- time.After(time.Second * 3):
t.Error("timout reached, no message received")
}
}

0 comments on commit dc129db

Please sign in to comment.