From b1f3ce9dd6862b0de0ded99403ff8b4a9662d9c6 Mon Sep 17 00:00:00 2001 From: Alexey Lavrenuke Date: Fri, 15 Jan 2016 17:29:22 +0300 Subject: [PATCH] test connect and ping --- gun/spdy/spdy.go | 26 ++++++++++++++----------- gun/spdy/spdy_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/gun/spdy/spdy.go b/gun/spdy/spdy.go index d99e4e972..fb1607542 100644 --- a/gun/spdy/spdy.go +++ b/gun/spdy/spdy.go @@ -27,18 +27,11 @@ type SpdyGun struct { func (sg *SpdyGun) Shoot(ctx context.Context, a ammo.Ammo, results chan<- *aggregate.Sample) error { if sg.client == nil { - if err := sg.connect(results); err != nil { + if err := sg.Connect(results); err != nil { return err } } - if sg.pingPeriod > 0 { - pingTimer := time.NewTicker(sg.pingPeriod) - go func() { - for range pingTimer.C { - sg.Ping(results) - } - }() - } + start := time.Now() ss := aggregate.AcquireSample(float64(start.UnixNano())/1e9, "REQUEST") defer func() { @@ -91,7 +84,7 @@ func (sg *SpdyGun) Close() { } } -func (sg *SpdyGun) connect(results chan<- *aggregate.Sample) error { +func (sg *SpdyGun) Connect(results chan<- *aggregate.Sample) error { // FIXME: rewrite connection logic, it isn't thread safe right now. start := time.Now() ss := aggregate.AcquireSample(float64(start.UnixNano())/1e9, "CONNECT") @@ -149,7 +142,7 @@ func (sg *SpdyGun) Ping(results chan<- *aggregate.Sample) { } results <- ss if err != nil { - sg.connect(results) + sg.Connect(results) } } @@ -185,5 +178,16 @@ func New(c *config.Gun) (gun.Gun, error) { return nil, fmt.Errorf("Target is of the wrong type."+ " Expected 'string' got '%T'", t) } + // TODO: implement this logic somewhere + // if pingPeriod > 0 { + // go func() { + // for range time.NewTicker(pingPeriod).C { + // if g.closed { + // return + // } + // g.Ping(results) + // } + // }() + // } return g, nil } diff --git a/gun/spdy/spdy_test.go b/gun/spdy/spdy_test.go index e843de802..7ba67bad0 100644 --- a/gun/spdy/spdy_test.go +++ b/gun/spdy/spdy_test.go @@ -28,6 +28,7 @@ func TestSpdyGun(t *testing.T) { pingPeriod: time.Second * 5, } promise := utils.Promise(func() error { + defer gun.Close() defer close(result) return gun.Shoot(ctx, &ammo.Http{ Host: "example.org", @@ -58,7 +59,6 @@ func TestSpdyGun(t *testing.T) { } // TODO: test scenaries with errors - // TODO: test ping logic select { case err := <-promise: @@ -69,6 +69,49 @@ func TestSpdyGun(t *testing.T) { } +func TestSpdyConnectPing(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + result := make(chan *aggregate.Sample) + + gun := &SpdyGun{ + target: "localhost:3000", + pingPeriod: time.Second * 5, + } + promise := utils.Promise(func() error { + defer gun.Close() + defer close(result) + if err := gun.Connect(result); err != nil { + return err + } + gun.Ping(result) + return nil + }) + + results := aggregate.Drain(ctx, result) + require.Len(t, results, 2) + { + // first result is connect + + assert.Equal(t, "CONNECT", results[0].Tag) + assert.Equal(t, 200, results[0].ProtoCode) + } + { + // second result is PING + + assert.Equal(t, "PING", results[1].Tag) + assert.Equal(t, 200, results[1].ProtoCode) + } + select { + case err := <-promise: + require.NoError(t, err) + case <-ctx.Done(): + t.Fatal(ctx.Err()) + } + +} + func runSpdyTestServer() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain")