Skip to content

Commit

Permalink
test connect and ping
Browse files Browse the repository at this point in the history
  • Loading branch information
direvius committed Jan 15, 2016
1 parent 9afb17d commit b1f3ce9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
26 changes: 15 additions & 11 deletions gun/spdy/spdy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -149,7 +142,7 @@ func (sg *SpdyGun) Ping(results chan<- *aggregate.Sample) {
}
results <- ss
if err != nil {
sg.connect(results)
sg.Connect(results)
}
}

Expand Down Expand Up @@ -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
}
45 changes: 44 additions & 1 deletion gun/spdy/spdy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -58,7 +59,6 @@ func TestSpdyGun(t *testing.T) {
}

// TODO: test scenaries with errors
// TODO: test ping logic

select {
case err := <-promise:
Expand All @@ -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")
Expand Down

0 comments on commit b1f3ce9

Please sign in to comment.