func initServerAndClients(t *testing.T) (*server.Service, *client.Client, *client.Client, func()) { service := StartupService(Args{Listen: "localhost:0", KVBackend: "memory"}) time.Sleep(time.Millisecond * 100) var err error client1, err := client.Open("ws://"+service.GetWebServer().GetAddr()+"/stream/user/user1", "http://localhost", 1, false) assert.NoError(t, err) checkConnectedNotificationJson(t, "user1", expectStatusMessage(t, client1, guble.SUCCESS_CONNECTED, "You are connected to the server."), ) client2, err := client.Open("ws://"+service.GetWebServer().GetAddr()+"/stream/user/user2", "http://localhost", 1, false) assert.NoError(t, err) checkConnectedNotificationJson(t, "user2", expectStatusMessage(t, client2, guble.SUCCESS_CONNECTED, "You are connected to the server."), ) return service, client1, client2, func() { service.Stop() if client1 != nil { client1.Close() } if client2 != nil { client2.Close() } } }
func initServerAndClients(t *testing.T) (*service.Service, client.Client, client.Client, func()) { *Config.HttpListen = "localhost:0" *Config.KVS = "memory" s := StartService() time.Sleep(time.Millisecond * 100) var err error client1, err := client.Open("ws://"+s.WebServer().GetAddr()+"/stream/user/user1", "http://localhost", 1, false) assert.NoError(t, err) checkConnectedNotificationJSON(t, "user1", expectStatusMessage(t, client1, protocol.SUCCESS_CONNECTED, "You are connected to the server."), ) client2, err := client.Open("ws://"+s.WebServer().GetAddr()+"/stream/user/user2", "http://localhost", 1, false) assert.NoError(t, err) checkConnectedNotificationJSON(t, "user2", expectStatusMessage(t, client2, protocol.SUCCESS_CONNECTED, "You are connected to the server."), ) return s, client1, client2, func() { if client1 != nil { client1.Close() } if client2 != nil { client2.Close() } s.Stop() } }
// This is a minimal commandline client to connect through a websocket func main() { guble.LogLevel = guble.LEVEL_ERR args = loadArgs() if args.LogInfo { guble.LogLevel = guble.LEVEL_INFO } if args.LogDebug { guble.LogLevel = guble.LEVEL_DEBUG } origin := "http://localhost/" url := fmt.Sprintf("%v/user/%v", removeTrailingSlash(args.Url), args.User) client, err := client.Open(url, origin, 100, true) if err != nil { log.Fatal(err) } go writeLoop(client) go readLoop(client) for _, cmd := range args.Commands { client.WriteRawMessage([]byte(cmd)) } if args.Exit { return } waitForTermination(func() {}) }
// This is a minimal commandline client to connect through a websocket func main() { kingpin.Parse() // set log level level, err := log.ParseLevel(*logLevel) if err != nil { logger.WithField("error", err).Fatal("Invalid log level") } log.SetLevel(level) origin := "http://localhost/" url := fmt.Sprintf("%v/user/%v", removeTrailingSlash(*url), *user) client, err := client.Open(url, origin, 100, true) if err != nil { log.Fatal(err) } go writeLoop(client) go readLoop(client) for _, cmd := range *commands { client.WriteRawMessage([]byte(cmd)) } if *exit { return } waitForTermination(func() {}) }
func (tcn *testClusterNode) client(userID string, bufferSize int, autoReconnect bool) (client.Client, error) { serverAddr := tcn.Service.WebServer().GetAddr() wsURL := "ws://" + serverAddr + "/stream/user/" + userID httpURL := "http://" + serverAddr return client.Open(wsURL, httpURL, bufferSize, autoReconnect) }
func (tg *testgroup) Init() { tg.topic = fmt.Sprintf("/%v-foo", tg.groupID) var err error location := "ws://" + tg.addr + "/stream/user/xy" //location := "ws://gathermon.mancke.net:8080/stream/" //location := "ws://127.0.0.1:8080/stream/" tg.client1, err = client.Open(location, "http://localhost/", 10, false) if err != nil { panic(err) } tg.client2, err = client.Open(location, "http://localhost/", 10, false) if err != nil { panic(err) } tg.expectStatusMessage(protocol.SUCCESS_CONNECTED, "You are connected to the server.") tg.client1.Subscribe(tg.topic) time.Sleep(time.Millisecond * 1) //test.expectStatusMessage(protocol.SUCCESS_SUBSCRIBED_TO, test.topic) }
func Benchmark_E2E_Fetch_HelloWorld_Messages(b *testing.B) { defer testutil.ResetDefaultRegistryHealthCheck() a := assert.New(b) dir, _ := ioutil.TempDir("", "guble_benchmarking_fetch_test") defer os.RemoveAll(dir) *Config.HttpListen = "localhost:0" *Config.KVS = "memory" *Config.MS = "file" *Config.StoragePath = dir service := StartService() defer service.Stop() time.Sleep(time.Millisecond * 10) // fill the topic location := "ws://" + service.WebServer().GetAddr() + "/stream/user/xy" c, err := client.Open(location, "http://localhost/", 1000, true) a.NoError(err) for i := 1; i <= b.N; i++ { a.NoError(c.Send("/hello", fmt.Sprintf("Hello %v", i), "")) select { case <-c.StatusMessages(): // wait for, but ignore case <-time.After(time.Millisecond * 100): a.Fail("timeout on send notification") return } } start := time.Now() b.ResetTimer() c.WriteRawMessage([]byte("+ /hello 0 1000000")) for i := 1; i <= b.N; i++ { select { case msg := <-c.Messages(): a.Equal(fmt.Sprintf("Hello %v", i), msg.BodyAsString()) case e := <-c.Errors(): a.Fail(string(e.Bytes())) return case <-time.After(time.Second): a.Fail("timeout on message: " + strconv.Itoa(i)) return } } b.StopTimer() end := time.Now() throughput := float64(b.N) / end.Sub(start).Seconds() fmt.Printf("\n\tThroughput: %v/sec (%v message in %v)\n", int(throughput), b.N, end.Sub(start)) }
func (params *benchParams) createClients() (clients []client.Client) { wsURL := "ws://" + params.service.WebServer().GetAddr() + "/stream/user/" for clientID := 0; clientID < params.clients; clientID++ { location := wsURL + strconv.Itoa(clientID) c, err := client.Open(location, "http://localhost/", 1000, true) if err != nil { assert.FailNow(params, "guble client could not connect to server") } clients = append(clients, c) } return }
func throughputSend(b *testing.B, nWorkers int, sampleSend func(c client.Client) error) float64 { //testutil.EnableDebugForMethod() fmt.Printf("b.N=%v\n", b.N) defer testutil.ResetDefaultRegistryHealthCheck() a := assert.New(b) dir, errTempDir := ioutil.TempDir("", "guble_benchmarking_gcm_test") defer func() { errRemove := os.RemoveAll(dir) if errRemove != nil { log.WithFields(log.Fields{ "module": "testing", "err": errRemove, }).Error("Could not remove directory") } }() a.NoError(errTempDir) *config.HttpListen = "localhost:0" *config.KVS = "memory" *config.MS = "file" *config.StoragePath = dir *config.GCM.Enabled = true *config.GCM.APIKey = "WILL BE OVERWRITTEN" *config.GCM.Workers = nWorkers service := StartService() log.WithFields(log.Fields{ "module": "testing", }).Debug("Overwriting the GCM Sender with a MocK") gcmConnector, ok := service.Modules()[4].(*gcm.GCMConnector) a.True(ok, "Modules[4] should be of type GCMConnector") gcmConnector.Sender = testutil.CreateGcmSender( testutil.CreateRoundTripperWithJsonResponse(http.StatusOK, testutil.CorrectGcmResponseMessageJSON, nil)) gomaxprocs := runtime.GOMAXPROCS(0) clients := make([]client.Client, 0, gomaxprocs) for clientID := 0; clientID < gomaxprocs; clientID++ { location := "ws://" + service.WebServer().GetAddr() + "/stream/user/" + strconv.Itoa(clientID) client, err := client.Open(location, "http://localhost/", 1000, true) a.NoError(err) clients = append(clients, client) } // create a topic url := fmt.Sprintf("http://%s/gcm/0/gcmId0/subscribe/topic", service.WebServer().GetAddr()) response, errPost := http.Post(url, "text/plain", bytes.NewBufferString("")) a.NoError(errPost) a.Equal(response.StatusCode, 200) body, errReadAll := ioutil.ReadAll(response.Body) a.NoError(errReadAll) a.Equal("registered: /topic\n", string(body)) log.WithFields(log.Fields{ "module": "testing", }).Debug("Overwriting the GCM Sender with a MocK") start := time.Now() b.ResetTimer() log.WithFields(log.Fields{ "module": "testing", }).Debug("Sending multiple messages from each client in separate goroutines") var wg sync.WaitGroup wg.Add(gomaxprocs) for _, c := range clients { go func(c client.Client) { defer wg.Done() for i := 0; i < b.N; i++ { a.NoError(sampleSend(c)) } }(c) } wg.Wait() // stop service (and wait for all the messages to be processed during the given grace period) err := service.Stop() a.Nil(err) end := time.Now() b.StopTimer() return float64(b.N*gomaxprocs) / end.Sub(start).Seconds() }
func clientSetUp(t *testing.T, service *service.Service) client.Client { wsURL := "ws://" + service.WebServer().GetAddr() + "/stream/user/user01" c, err := client.Open(wsURL, "http://localhost/", 1000, false) assert.NoError(t, err) return c }