summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/braintree/manners/transition_test.go
blob: ffa06d3eb22a2bbdb157ed6949a1b20f1c9cd583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package manners

import (
	"net/http"
	"strings"
	"testing"
)

func TestStateTransitions(t *testing.T) {
	tests := []transitionTest{
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive}, 1},
		transitionTest{[]http.ConnState{http.StateNew, http.StateClosed}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateClosed}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateHijacked}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateIdle}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateIdle, http.StateActive}, 1},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateIdle, http.StateActive, http.StateIdle}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateIdle, http.StateActive, http.StateClosed}, 0},
		transitionTest{[]http.ConnState{http.StateNew, http.StateActive, http.StateIdle, http.StateActive, http.StateIdle, http.StateClosed}, 0},
	}

	for _, test := range tests {
		testStateTransition(t, test)
	}
}

type transitionTest struct {
	states          []http.ConnState
	expectedWgCount int
}

func testStateTransition(t *testing.T, test transitionTest) {
	server := newServer()
	wg := newTestWg()
	server.wg = wg
	startServer(t, server, nil)

	conn := &fakeConn{}
	for _, newState := range test.states {
		server.ConnState(conn, newState)
	}

	server.Close()
	waiting := <-wg.waitCalled
	if waiting != test.expectedWgCount {
		names := make([]string, len(test.states))
		for i, s := range test.states {
			names[i] = s.String()
		}
		transitions := strings.Join(names, " -> ")
		t.Errorf("%s - Waitcount should be %d, got %d", transitions, test.expectedWgCount, waiting)
	}
}