github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/actor/receiver_http_test.go (about) 1 package actor 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "net/url" 9 "testing" 10 "time" 11 12 "github.com/awesome-flow/flow/pkg/cfg" 13 core "github.com/awesome-flow/flow/pkg/corev1alpha1" 14 testutil "github.com/awesome-flow/flow/pkg/util/test" 15 flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1" 16 ) 17 18 func TestHandleReqV1alpha1(t *testing.T) { 19 nthreads := 4 20 21 type teststruct struct { 22 name string 23 url string 24 reqbody []byte 25 msgstatus core.MsgStatus 26 lag time.Duration 27 expcode int 28 expresp string 29 } 30 31 tests := make([]teststruct, 0, len(MsgStatusToHttpResp)+1) 32 33 for sts, ct := range MsgStatusToHttpResp { 34 tests = append(tests, teststruct{ 35 name: fmt.Sprintf("%s to %d %s", sts2name(sts), ct.code, ct.text), 36 url: "http://example.com", 37 reqbody: testutil.RandBytes(1024), 38 msgstatus: sts, 39 expcode: ct.code, 40 expresp: string(ct.text), 41 }) 42 } 43 44 lag := 100 * time.Millisecond 45 tests = append(tests, teststruct{ 46 name: fmt.Sprintf("lag of %d to %d %s", lag, http.StatusGatewayTimeout, "Timed out to send message"), 47 url: "http://example.com", 48 reqbody: testutil.RandBytes(1024), 49 msgstatus: core.MsgStatusTimedOut, 50 lag: lag, 51 expcode: http.StatusGatewayTimeout, 52 expresp: "Timed out to send message", 53 }) 54 55 t.Parallel() 56 57 for _, testCase := range tests { 58 t.Run(testCase.name, func(t *testing.T) { 59 url, err := url.Parse(testCase.url) 60 if err != nil { 61 t.Fatalf("failed to parse url %q: %s", testCase.url, err) 62 } 63 64 req := &http.Request{ 65 Body: ioutil.NopCloser(bytes.NewBuffer(testCase.reqbody)), 66 ContentLength: int64(len(testCase.reqbody)), 67 URL: url, 68 } 69 70 repo := cfg.NewRepository() 71 ctx, err := core.NewContext(core.NewConfig(repo)) 72 if err != nil { 73 t.Fatalf("failed to create context: %s", err) 74 } 75 if err := ctx.Start(); err != nil { 76 t.Fatalf("failed to start context: %s", err) 77 } 78 79 rcv, err := NewReceiverHTTP( 80 "receiver-http", 81 ctx, 82 core.Params{"bind": "0.0.0.0:8080"}, // bind is a mock, we are not starting the server 83 ) 84 if err != nil { 85 t.Fatalf("failed to create receiver: %s", err) 86 } 87 88 peer, err := flowtest.NewTestActor("test-actor", ctx, core.Params{}) 89 if err != nil { 90 t.Fatalf("failed to create test actor: %s", err) 91 } 92 if err := rcv.Connect(nthreads, peer); err != nil { 93 t.Fatalf("failed to connect test actor: %s", err) 94 } 95 peer.(*flowtest.TestActor).OnReceive(func(msg *core.Message) { 96 if testCase.lag > 0 { 97 time.Sleep(testCase.lag) 98 } 99 msg.Complete(testCase.msgstatus) 100 peer.(*flowtest.TestActor).Flush() 101 }) 102 103 rw := &testResponseWriter{} 104 rcv.(*ReceiverHTTP).handleReqV1alpha1(rw, req) 105 106 if rw.status != testCase.expcode { 107 t.Fatalf("unexpected http status: got: %d, want: %d", rw.status, http.StatusOK) 108 } 109 110 if resp := rw.String(); resp != testCase.expresp { 111 t.Fatalf("unexpected http response: got: %s, want: %s", resp, "OK") 112 } 113 }) 114 } 115 }