|
|
@@ -36,8 +36,11 @@ const (
|
|
|
_SERVER_ID = "myserverid"
|
|
|
)
|
|
|
|
|
|
+var nextServerID = 0
|
|
|
+
|
|
|
type StatsTestSuite struct {
|
|
|
suite.Suite
|
|
|
+ serverID string
|
|
|
httpClient *http.Client
|
|
|
}
|
|
|
|
|
|
@@ -47,9 +50,11 @@ func TestStatsTestSuite(t *testing.T) {
|
|
|
|
|
|
func (suite *StatsTestSuite) SetupTest() {
|
|
|
re := make(Regexps, 0)
|
|
|
+ suite.serverID = fmt.Sprintf("%s-%d", _SERVER_ID, nextServerID)
|
|
|
+ nextServerID++
|
|
|
suite.httpClient = &http.Client{
|
|
|
Transport: &http.Transport{
|
|
|
- Dial: makeStatsDialer(_SERVER_ID, &re),
|
|
|
+ Dial: makeStatsDialer(suite.serverID, &re),
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
@@ -91,23 +96,20 @@ func (suite *StatsTestSuite) Test_StatsConn() {
|
|
|
resp, err = suite.httpClient.Get("https://example.org/index.html")
|
|
|
suite.Nil(err, "basic HTTPS requests should succeed")
|
|
|
resp.Body.Close()
|
|
|
-
|
|
|
- // Clear out stats
|
|
|
- _ = TakeOutStatsForServer(_SERVER_ID)
|
|
|
}
|
|
|
|
|
|
func (suite *StatsTestSuite) Test_TakeOutStatsForServer() {
|
|
|
|
|
|
zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
|
|
|
|
|
|
- payload := TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload := TakeOutStatsForServer(suite.serverID)
|
|
|
suite.Equal(payload, zeroPayload, "should get zero stats before any traffic")
|
|
|
|
|
|
resp, err := suite.httpClient.Get("http://example.com/index.html")
|
|
|
suite.Nil(err, "need successful http to proceed with tests")
|
|
|
resp.Body.Close()
|
|
|
|
|
|
- payload = TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload = TakeOutStatsForServer(suite.serverID)
|
|
|
suite.NotNil(payload, "should receive valid payload for valid server ID")
|
|
|
|
|
|
payloadJSON, err := json.Marshal(payload)
|
|
|
@@ -116,7 +118,7 @@ func (suite *StatsTestSuite) Test_TakeOutStatsForServer() {
|
|
|
suite.Nil(err, "payload JSON should parse successfully")
|
|
|
|
|
|
// After we retrieve the stats for a server, they should be cleared out of the tracked stats
|
|
|
- payload = TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload = TakeOutStatsForServer(suite.serverID)
|
|
|
suite.Equal(payload, zeroPayload, "after retrieving stats for a server, there should be zero stats (until more data goes through)")
|
|
|
}
|
|
|
|
|
|
@@ -125,19 +127,19 @@ func (suite *StatsTestSuite) Test_PutBackStatsForServer() {
|
|
|
suite.Nil(err, "need successful http to proceed with tests")
|
|
|
resp.Body.Close()
|
|
|
|
|
|
- payloadToPutBack := TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payloadToPutBack := TakeOutStatsForServer(suite.serverID)
|
|
|
suite.NotNil(payloadToPutBack, "should receive valid payload for valid server ID")
|
|
|
|
|
|
zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
|
|
|
|
|
|
- payload := TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload := TakeOutStatsForServer(suite.serverID)
|
|
|
suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
|
|
|
|
|
|
- PutBackStatsForServer(_SERVER_ID, payloadToPutBack)
|
|
|
+ PutBackStatsForServer(suite.serverID, payloadToPutBack)
|
|
|
// PutBack is asynchronous, so we'll need to wait a moment for it to do its thing
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
|
|
- payload = TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload = TakeOutStatsForServer(suite.serverID)
|
|
|
suite.NotEqual(payload, zeroPayload, "stats should be re-added after putting back")
|
|
|
suite.Equal(payload, payloadToPutBack, "stats should be the same as after the first retrieval")
|
|
|
}
|
|
|
@@ -196,7 +198,7 @@ func (suite *StatsTestSuite) Test_Regex() {
|
|
|
|
|
|
suite.httpClient = &http.Client{
|
|
|
Transport: &http.Transport{
|
|
|
- Dial: makeStatsDialer(_SERVER_ID, regexps),
|
|
|
+ Dial: makeStatsDialer(suite.serverID, regexps),
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -220,7 +222,7 @@ func (suite *StatsTestSuite) Test_Regex() {
|
|
|
suite.Nil(err)
|
|
|
resp.Body.Close()
|
|
|
|
|
|
- payload := TakeOutStatsForServer(_SERVER_ID)
|
|
|
+ payload := TakeOutStatsForServer(suite.serverID)
|
|
|
suite.NotNil(payload, "should get stats because we made HTTP reqs; %s", scheme)
|
|
|
|
|
|
expectedHostnames := mapset.NewSet()
|
|
|
@@ -251,7 +253,7 @@ func (suite *StatsTestSuite) Test_getTLSHostname() {
|
|
|
|
|
|
// TODO: Talk to a local TCP server instead of spamming example.com
|
|
|
|
|
|
- dialer := makeStatsDialer(_SERVER_ID, nil)
|
|
|
+ dialer := makeStatsDialer(suite.serverID, nil)
|
|
|
|
|
|
// Data too short
|
|
|
conn, err := dialer("tcp", "example.com:80")
|