github.com/philippseith/signalr@v0.6.3/server_test.go (about) 1 package signalr 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("Server.HubClients", func() { 13 Context("All().Send()", func() { 14 j := 1 15 It(fmt.Sprintf("should send clients %v", j), func(done Done) { 16 // Create a simple server 17 server, err := NewServer(context.TODO(), SimpleHubFactory(&simpleHub{}), 18 testLoggerOption(), 19 ChanReceiveTimeout(200*time.Millisecond), 20 StreamBufferCapacity(5)) 21 Expect(err).NotTo(HaveOccurred()) 22 Expect(server).NotTo(BeNil()) 23 // Create both ends of the connection 24 cliConn, srvConn := newClientServerConnections() 25 // Start the server 26 go func() { _ = server.Serve(srvConn) }() 27 // Give the server some time. In contrast to the client, we have not connected state to query 28 <-time.After(100 * time.Millisecond) 29 // Create the Client 30 receiver := &simpleReceiver{ch: make(chan string, 1)} 31 ctx, cancelClient := context.WithCancel(context.Background()) 32 client, _ := NewClient(ctx, 33 WithConnection(cliConn), 34 WithReceiver(receiver), 35 testLoggerOption(), 36 TransferFormat("Text")) 37 Expect(client).NotTo(BeNil()) 38 // Start it 39 client.Start() 40 // Wait for client running 41 Expect(<-client.WaitForState(context.Background(), ClientConnected)).NotTo(HaveOccurred()) 42 // Send from the server to "all" clients 43 <-time.After(100 * time.Millisecond) 44 server.HubClients().All().Send("OnCallback", fmt.Sprintf("All%v", j)) 45 // Did the receiver get what we did send? 46 Expect(<-receiver.ch).To(Equal(fmt.Sprintf("All%v", j))) 47 cancelClient() 48 server.cancel() 49 close(done) 50 }, 1.0) 51 }) 52 53 Context("Caller()", func() { 54 It("should return nil", func() { 55 server, _ := NewServer(context.TODO(), SimpleHubFactory(&simpleHub{}), 56 testLoggerOption(), 57 ChanReceiveTimeout(200*time.Millisecond), 58 StreamBufferCapacity(5)) 59 Expect(server.HubClients().Caller()).To(BeNil()) 60 }) 61 }) 62 })