func generateSmallMessages(n int) []proto.Message { queue := make([]proto.Message, n) for i := range queue { queue[i] = testmessage.GenerateSmallMessage() } return queue }
func TestTransporter_DiscardedSend(t *testing.T) { serverId := "testserver" // setup mesos client-side protoMsg := testmessage.GenerateSmallMessage() msgName := getMessageName(protoMsg) msg := &Message{ Name: msgName, ProtoMessage: protoMsg, } requestURI := fmt.Sprintf("/%s/%s", serverId, msgName) // setup server-side msgReceived := make(chan struct{}) srv := makeMockServer(requestURI, func(rsp http.ResponseWriter, req *http.Request) { close(msgReceived) time.Sleep(2 * time.Second) // long enough that we should be able to stop it }) defer srv.Close() toUpid, err := upid.Parse(fmt.Sprintf("%s@%s", serverId, srv.Listener.Addr().String())) assert.NoError(t, err) // make transport call. transport := NewHTTPTransporter(upid.UPID{ID: "mesos1", Host: "localhost"}, nil) _, errch := transport.Start() defer transport.Stop(false) msg.UPID = toUpid senderr := make(chan struct{}) go func() { defer close(senderr) err = transport.Send(context.TODO(), msg) assert.NotNil(t, err) assert.Equal(t, discardOnStopError, err) }() // wait for message to be received select { case <-time.After(2 * time.Second): t.Fatalf("timed out waiting for message receipt") return case <-msgReceived: transport.Stop(false) case err := <-errch: if err != nil { t.Fatalf(err.Error()) return } } // wait for send() to process discarded-error select { case <-time.After(5 * time.Second): t.Fatalf("timed out waiting for aborted send") return case <-senderr: // continue } }
func TestTransporterStartAndRcvd(t *testing.T) { serverId := "testserver" serverAddr := "127.0.0.1" protoMsg := testmessage.GenerateSmallMessage() msgName := getMessageName(protoMsg) ctrl := make(chan struct{}) // setup receiver (server) process receiver := NewHTTPTransporter(upid.UPID{ID: serverId, Host: serverAddr}, nil) receiver.Install(msgName) go func() { defer close(ctrl) t.Logf("received something...") msg, err := receiver.Recv() assert.Nil(t, err) assert.NotNil(t, msg) if msg != nil { assert.Equal(t, msgName, msg.Name) } }() rcvPid, errch := receiver.Start() defer receiver.Stop(false) assert.NotNil(t, errch) time.Sleep(time.Millisecond * 7) // time to catchup // setup sender (client) process sender := NewHTTPTransporter(upid.UPID{ID: "mesos1", Host: "localhost"}, nil) msg := &Message{ UPID: &rcvPid, Name: msgName, ProtoMessage: protoMsg, } _, errch2 := sender.Start() defer sender.Stop(false) t.Logf("sending test message") sender.Send(context.TODO(), msg) select { case <-time.After(time.Second * 5): t.Fatalf("Timeout") case <-ctrl: case err := <-errch: if err != nil { t.Fatalf(err.Error()) } case err := <-errch2: if err != nil { t.Fatalf(err.Error()) } } }
func generateMixedMessages(n int) []proto.Message { queue := make([]proto.Message, n*4) for i := 0; i < n*4; i = i + 4 { queue[i] = testmessage.GenerateSmallMessage() queue[i+1] = testmessage.GenerateMediumMessage() queue[i+2] = testmessage.GenerateBigMessage() queue[i+3] = testmessage.GenerateLargeMessage() } shuffleMessages(&queue) return queue }
func TestTransporterSend(t *testing.T) { idreg := regexp.MustCompile(`[A-Za-z0-9_\-]+@[A-Za-z0-9_\-\.]+:[0-9]+`) serverId := "testserver" // setup mesos client-side protoMsg := testmessage.GenerateSmallMessage() msgName := getMessageName(protoMsg) msg := &Message{ Name: msgName, ProtoMessage: protoMsg, } requestURI := fmt.Sprintf("/%s/%s", serverId, msgName) // setup server-side msgReceived := make(chan struct{}) srv := makeMockServer(requestURI, func(rsp http.ResponseWriter, req *http.Request) { defer close(msgReceived) from := req.Header.Get("Libprocess-From") assert.NotEmpty(t, from) assert.True(t, idreg.MatchString(from), fmt.Sprintf("regexp failed for '%v'", from)) }) defer srv.Close() toUpid, err := upid.Parse(fmt.Sprintf("%s@%s", serverId, srv.Listener.Addr().String())) assert.NoError(t, err) // make transport call. transport := NewHTTPTransporter(upid.UPID{ID: "mesos1", Host: "localhost"}, nil) _, errch := transport.Start() defer transport.Stop(false) msg.UPID = toUpid err = transport.Send(context.TODO(), msg) assert.NoError(t, err) select { case <-time.After(2 * time.Second): t.Fatalf("timed out waiting for message receipt") case <-msgReceived: case err := <-errch: if err != nil { t.Fatalf(err.Error()) } } }
func TestTransporterStartAndInject(t *testing.T) { serverId := "testserver" protoMsg := testmessage.GenerateSmallMessage() msgName := getMessageName(protoMsg) ctrl := make(chan struct{}) // setup receiver (server) process receiver := NewHTTPTransporter(upid.UPID{ID: serverId, Host: "127.0.0.1"}, nil) receiver.Install(msgName) rcvPid, errch := receiver.Start() defer receiver.Stop(false) msg := &Message{ UPID: &rcvPid, Name: msgName, ProtoMessage: protoMsg, } receiver.Inject(context.TODO(), msg) go func() { defer close(ctrl) msg, err := receiver.Recv() assert.Nil(t, err) assert.NotNil(t, msg) if msg != nil { assert.Equal(t, msgName, msg.Name) } }() select { case <-time.After(time.Second * 1): t.Fatalf("Timeout") case <-ctrl: case err := <-errch: if err != nil { t.Fatalf(err.Error()) } } }