From 0e3440bc35a6c09f03c1bc11d2bd7cfdffc4ab15 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Tue, 13 Jan 2015 13:29:52 -0300 Subject: [PATCH] Fixed app and connection tests. The tests was not updated. --- app_test.go | 62 +++++++++++++++++++++++++++++----------------------- conn.go | 6 ++--- conn_test.go | 10 ++++----- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/app_test.go b/app_test.go index 6e6009f..0e960b1 100644 --- a/app_test.go +++ b/app_test.go @@ -5,11 +5,19 @@ package main import ( + "strconv" "testing" ) +var id = 0 + func newApp() App { - return App{Name: "Test", AppID: "123", Key: "123", Secret: "123", OnlySSL: false, ApplicationDisabled: false, UserEvents: true} + + a := App{Name: "Test", AppID: strconv.Itoa(id), Key: "123", Secret: "123", OnlySSL: false, ApplicationDisabled: false, UserEvents: true} + a.Init() + + id++ + return a } func Test_add_channels(t *testing.T) { @@ -18,37 +26,37 @@ func Test_add_channels(t *testing.T) { // Public - if len(app.PublicChannels) != 0 { + if len(app.PublicChannels()) != 0 { t.Error("Length of public channels must be 0 before test") } - app.AddChannel(NewChannel("ID", "")) + app.AddChannel(NewChannel("ID")) - if len(app.PublicChannels) != 1 { + if len(app.PublicChannels()) != 1 { t.Error("Length os public channels after insert must be 1") } // Presence - if len(app.PresenceChannels) != 0 { + if len(app.PresenceChannels()) != 0 { t.Error("Length of presence channels must be 0 before test") } - app.AddChannel(NewChannel("presence-test", "")) + app.AddChannel(NewChannel("presence-test")) - if len(app.PresenceChannels) != 1 { + if len(app.PresenceChannels()) != 1 { t.Error("Length os presence channels after insert must be 1") } // Private - if len(app.PrivateChannels) != 0 { + if len(app.PrivateChannels()) != 0 { t.Error("Length of private channels must be 0 before test") } - app.AddChannel(NewChannel("private-test", "")) + app.AddChannel(NewChannel("private-test")) - if len(app.PrivateChannels) != 1 { + if len(app.PrivateChannels()) != 1 { t.Error("Length os private channels after insert must be 1") } @@ -56,11 +64,11 @@ func Test_add_channels(t *testing.T) { func Test_AllChannels(t *testing.T) { app := newApp() - app.AddChannel(NewChannel("private-test", "")) - app.AddChannel(NewChannel("presence-test", "")) - app.AddChannel(NewChannel("test", "")) + app.AddChannel(NewChannel("private-test")) + app.AddChannel(NewChannel("presence-test")) + app.AddChannel(NewChannel("test")) - if len(app.AllChannels()) != 3 { + if len(app.Channels) != 3 { t.Error("Must have 3 channels") } } @@ -72,8 +80,8 @@ func Test_New_Subscriber(t *testing.T) { t.Error("Length of subscribers before test must be 0") } - conn := NewSubscriber("1", "", nil) - app.AddSubscriber(conn) + conn := NewSubscriber("1", nil) + app.Connect(conn) if len(app.Subscribers) != 1 { t.Error("Length os subscribers after test must be 1") @@ -82,8 +90,8 @@ func Test_New_Subscriber(t *testing.T) { func Test_find_subscriber(t *testing.T) { app := newApp() - conn := NewSubscriber("1", "", nil) - app.AddSubscriber(conn) + conn := NewSubscriber("1", nil) + app.Connect(conn) conn, err := app.FindSubscriber("1") @@ -112,13 +120,13 @@ func Test_find_or_create_channels(t *testing.T) { app := newApp() // Public - if len(app.PublicChannels) != 0 { + if len(app.PublicChannels()) != 0 { t.Error("Length of public channels must be 0 before test") } - c := app.FindOrCreateChannelByChannelID("id", "") + c := app.FindOrCreateChannelByChannelID("id") - if len(app.PublicChannels) != 1 { + if len(app.PublicChannels()) != 1 { t.Error("Length os public channels after insert must be 1") } @@ -127,13 +135,13 @@ func Test_find_or_create_channels(t *testing.T) { } // Presence - if len(app.PresenceChannels) != 0 { + if len(app.PresenceChannels()) != 0 { t.Error("Length of presence channels must be 0 before test") } - c = app.FindOrCreateChannelByChannelID("presence-test", "") + c = app.FindOrCreateChannelByChannelID("presence-test") - if len(app.PresenceChannels) != 1 { + if len(app.PresenceChannels()) != 1 { t.Error("Length os presence channels after insert must be 1") } @@ -142,13 +150,13 @@ func Test_find_or_create_channels(t *testing.T) { } // Private - if len(app.PrivateChannels) != 0 { + if len(app.PrivateChannels()) != 0 { t.Error("Length of private channels must be 0 before test") } - c = app.FindOrCreateChannelByChannelID("private-test", "") + c = app.FindOrCreateChannelByChannelID("private-test") - if len(app.PrivateChannels) != 1 { + if len(app.PrivateChannels()) != 1 { t.Error("Length os private channels after insert must be 1") } diff --git a/conn.go b/conn.go index 0d05eef..7f8cd74 100644 --- a/conn.go +++ b/conn.go @@ -164,18 +164,18 @@ func NewChannel(channelID string) *Channel { } // This function generate a sequencial ID -func newID() string { +func newID() (string, int) { mutex.Lock() defer mutex.Unlock() currentID += 1 - return strconv.Itoa(currentID) + return strconv.Itoa(currentID), currentID } // Create a new Subscriber func NewSubscriber(socketID string, s *websocket.Conn) *Subscriber { - id := newID() + id, _ := newID() log.Infof("Creating a new Subscriber %+v with id %s", socketID, id) diff --git a/conn_test.go b/conn_test.go index 9dfb9e2..8e41e6c 100644 --- a/conn_test.go +++ b/conn_test.go @@ -4,14 +4,12 @@ package main -import ( - "testing" -) +import "testing" func Test_New_ID(t *testing.T) { - id := newID() + _, id := newID() - if newID() != id+1 { - t.Error("Every call to newID must increment the id by one") + if _, i := newID(); i != id+1 { + t.Errorf("Every call to newID must increment the id by one, got: %s", i) } }