-
Notifications
You must be signed in to change notification settings - Fork 379
test(pingpong): add synctest to ping test #5264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
f6a14f8
4a95156
f917c4f
bc444ee
a3222b6
cc7e940
be36092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,106 +8,95 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
| "testing/synctest" | ||
|
|
||
| "github.com/ethersphere/bee/v2/pkg/swarm" | ||
|
|
||
| "github.com/ethersphere/bee/v2/pkg/log" | ||
| "github.com/ethersphere/bee/v2/pkg/p2p" | ||
| "github.com/ethersphere/bee/v2/pkg/p2p/protobuf" | ||
| "github.com/ethersphere/bee/v2/pkg/p2p/streamtest" | ||
| "github.com/ethersphere/bee/v2/pkg/pingpong" | ||
| "github.com/ethersphere/bee/v2/pkg/pingpong/pb" | ||
| ) | ||
|
|
||
| func TestPing(t *testing.T) { | ||
| t.Parallel() | ||
| synctest.Test(t, func(t *testing.T) { | ||
| logger := log.Noop | ||
|
|
||
| logger := log.Noop | ||
| // create a pingpong server that handles the incoming stream | ||
| server := pingpong.New(nil, logger, nil) | ||
|
|
||
| // create a pingpong server that handles the incoming stream | ||
| server := pingpong.New(nil, logger, nil) | ||
| // setup the stream recorder to record stream data | ||
| recorder := streamtest.New( | ||
| streamtest.WithProtocols(server.Protocol()), | ||
| ) | ||
|
|
||
| // setup the stream recorder to record stream data | ||
| recorder := streamtest.New( | ||
| streamtest.WithProtocols(server.Protocol()), | ||
| streamtest.WithMiddlewares(func(f p2p.HandlerFunc) p2p.HandlerFunc { | ||
| if runtime.GOOS == "windows" { | ||
| // windows has a bit lower time resolution | ||
| // so, slow down the handler with a middleware | ||
| // not to get 0s for rtt value | ||
| time.Sleep(100 * time.Millisecond) | ||
| } | ||
| return f | ||
| }), | ||
| ) | ||
| // create a pingpong client that will do pinging | ||
| client := pingpong.New(recorder, logger, nil) | ||
|
|
||
| // create a pingpong client that will do pinging | ||
| client := pingpong.New(recorder, logger, nil) | ||
| // ping | ||
| addr := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c") | ||
| greetings := []string{"hey", "there", "fella"} | ||
| rtt, err := client.Ping(context.Background(), addr, greetings...) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // ping | ||
| addr := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c") | ||
| greetings := []string{"hey", "there", "fella"} | ||
| rtt, err := client.Ping(context.Background(), addr, greetings...) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| // check that RTT is a same value (rtt can be 0 in synctest virtual time) | ||
| if rtt < 0 { | ||
| t.Errorf("invalid RTT value %v", rtt) | ||
| } | ||
|
|
||
| // check that RTT is a sane value | ||
| if rtt <= 0 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test must evaluate that the rtt is not 0 as well, as zero value can also signal that the protocol does not work. It could be now made that the protocol waits when responding to messages with the middleware for any os (to remove the goos check), that and to check if the rtt value is measured correctly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to check the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, we have to make the time to advance in order to measure rtt in the bubble, by blocking something. I thought thought that it is good to use the middleware that you removed that sleeps, but for any os. Test is working with that approach, but it actually does not make the test correct as it measures the middleware, and we want to measure messages reading and writing latencies. So, I have added the option to recorder to introduce the message latency so that we can measure rtt, now even exactly, which is much better than before.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, good.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Updated. |
||
| t.Errorf("invalid RTT value %v", rtt) | ||
| } | ||
| // get a record for this stream | ||
| records, err := recorder.Records(addr, "pingpong", "1.0.0", "pingpong") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if l := len(records); l != 1 { | ||
| t.Fatalf("got %v records, want %v", l, 1) | ||
| } | ||
| record := records[0] | ||
|
|
||
| // get a record for this stream | ||
| records, err := recorder.Records(addr, "pingpong", "1.0.0", "pingpong") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if l := len(records); l != 1 { | ||
| t.Fatalf("got %v records, want %v", l, 1) | ||
| } | ||
| record := records[0] | ||
| // validate received ping greetings from the client | ||
| wantGreetings := greetings | ||
| messages, err := protobuf.ReadMessages( | ||
| bytes.NewReader(record.In()), | ||
| func() protobuf.Message { return new(pb.Ping) }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| gotGreetings := make([]string, 0, len(messages)) | ||
| for _, m := range messages { | ||
| gotGreetings = append(gotGreetings, m.(*pb.Ping).Greeting) | ||
| } | ||
| if fmt.Sprint(gotGreetings) != fmt.Sprint(wantGreetings) { | ||
| t.Errorf("got greetings %v, want %v", gotGreetings, wantGreetings) | ||
| } | ||
|
|
||
| // validate received ping greetings from the client | ||
| wantGreetings := greetings | ||
| messages, err := protobuf.ReadMessages( | ||
| bytes.NewReader(record.In()), | ||
| func() protobuf.Message { return new(pb.Ping) }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| gotGreetings := make([]string, 0, len(messages)) | ||
| for _, m := range messages { | ||
| gotGreetings = append(gotGreetings, m.(*pb.Ping).Greeting) | ||
| } | ||
| if fmt.Sprint(gotGreetings) != fmt.Sprint(wantGreetings) { | ||
| t.Errorf("got greetings %v, want %v", gotGreetings, wantGreetings) | ||
| } | ||
| // validate sent pong responses by handler | ||
| wantResponses := make([]string, 0, len(greetings)) | ||
| for _, g := range greetings { | ||
| wantResponses = append(wantResponses, "{"+g+"}") | ||
| } | ||
| messages, err = protobuf.ReadMessages( | ||
| bytes.NewReader(record.Out()), | ||
| func() protobuf.Message { return new(pb.Pong) }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| gotResponses := make([]string, 0, len(messages)) | ||
| for _, m := range messages { | ||
| gotResponses = append(gotResponses, m.(*pb.Pong).Response) | ||
| } | ||
| if fmt.Sprint(gotResponses) != fmt.Sprint(wantResponses) { | ||
| t.Errorf("got responses %v, want %v", gotResponses, wantResponses) | ||
| } | ||
|
|
||
| // validate sent pong responses by handler | ||
| wantResponses := make([]string, 0, len(greetings)) | ||
| for _, g := range greetings { | ||
| wantResponses = append(wantResponses, "{"+g+"}") | ||
| } | ||
| messages, err = protobuf.ReadMessages( | ||
| bytes.NewReader(record.Out()), | ||
| func() protobuf.Message { return new(pb.Pong) }, | ||
| ) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| gotResponses := make([]string, 0, len(messages)) | ||
| for _, m := range messages { | ||
| gotResponses = append(gotResponses, m.(*pb.Pong).Response) | ||
| } | ||
| if fmt.Sprint(gotResponses) != fmt.Sprint(wantResponses) { | ||
| t.Errorf("got responses %v, want %v", gotResponses, wantResponses) | ||
| } | ||
|
|
||
| if err := record.Err(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := record.Err(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'same' to 'sane' in comment.