Просмотр исходного кода

Update transferstats tests

- Fix: Now need a regex to get any domain
  bytes data.

- Added an explicit no-regex test to ensure
  no domain bytes are recorded in that case.
Rod Hynes 8 лет назад
Родитель
Сommit
d90419fee6
1 измененных файлов с 32 добавлено и 7 удалено
  1. 32 7
      psiphon/transferstats/transferstats_test.go

+ 32 - 7
psiphon/transferstats/transferstats_test.go

@@ -25,8 +25,8 @@ import (
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
+	"regexp"
 	"testing"
 	"testing"
-	"time"
 
 
 	mapset "github.com/deckarep/golang-set"
 	mapset "github.com/deckarep/golang-set"
 	"github.com/stretchr/testify/suite"
 	"github.com/stretchr/testify/suite"
@@ -40,8 +40,9 @@ var nextServerID = 0
 
 
 type StatsTestSuite struct {
 type StatsTestSuite struct {
 	suite.Suite
 	suite.Suite
-	serverID   string
-	httpClient *http.Client
+	serverID            string
+	httpClient          *http.Client
+	httpClientNoRegexes *http.Client
 }
 }
 
 
 func TestStatsTestSuite(t *testing.T) {
 func TestStatsTestSuite(t *testing.T) {
@@ -49,12 +50,23 @@ func TestStatsTestSuite(t *testing.T) {
 }
 }
 
 
 func (suite *StatsTestSuite) SetupTest() {
 func (suite *StatsTestSuite) SetupTest() {
-	re := make(Regexps, 0)
+
+	// Set a regexp for the httpClient to ensure it at least records "(OTHER)" domain bytes
+	regexp, _ := regexp.Compile(`^[a-z0-9\.]*\.(example\.com)$`)
+	replace := "$1"
+	regexps := Regexps{regexpReplace{regexp: regexp, replace: replace}}
+
 	suite.serverID = fmt.Sprintf("%s-%d", _SERVER_ID, nextServerID)
 	suite.serverID = fmt.Sprintf("%s-%d", _SERVER_ID, nextServerID)
 	nextServerID++
 	nextServerID++
 	suite.httpClient = &http.Client{
 	suite.httpClient = &http.Client{
 		Transport: &http.Transport{
 		Transport: &http.Transport{
-			Dial: makeStatsDialer(suite.serverID, &re),
+			Dial: makeStatsDialer(suite.serverID, &regexps),
+		},
+	}
+
+	suite.httpClientNoRegexes = &http.Client{
+		Transport: &http.Transport{
+			Dial: makeStatsDialer(suite.serverID, &Regexps{}),
 		},
 		},
 	}
 	}
 }
 }
@@ -136,14 +148,27 @@ func (suite *StatsTestSuite) Test_PutBackStatsForServer() {
 	suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
 	suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
 
 
 	PutBackStatsForServer(suite.serverID, 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(suite.serverID)
 	payload = TakeOutStatsForServer(suite.serverID)
 	suite.NotEqual(payload, zeroPayload, "stats should be re-added after putting back")
 	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")
 	suite.Equal(payload, payloadToPutBack, "stats should be the same as after the first retrieval")
 }
 }
 
 
+func (suite *StatsTestSuite) Test_NoRegexes() {
+
+	// Ensure there are no stats before making the no-regex request
+	_ = TakeOutStatsForServer(suite.serverID)
+
+	resp, err := suite.httpClientNoRegexes.Get("http://example.com/index.html")
+	suite.Nil(err, "need successful http to proceed with tests")
+	resp.Body.Close()
+
+	zeroPayload := &AccumulatedStats{hostnameToStats: make(map[string]*hostStats)}
+
+	payload := TakeOutStatsForServer(suite.serverID)
+	suite.Equal(payload, zeroPayload, "should be zero stats after getting them")
+}
+
 func (suite *StatsTestSuite) Test_MakeRegexps() {
 func (suite *StatsTestSuite) Test_MakeRegexps() {
 	pageViewRegexes := []map[string]string{make(map[string]string)}
 	pageViewRegexes := []map[string]string{make(map[string]string)}
 	pageViewRegexes[0]["regex"] = `(^http://[a-z0-9\.]*\.example\.[a-z\.]*)/.*`
 	pageViewRegexes[0]["regex"] = `(^http://[a-z0-9\.]*\.example\.[a-z\.]*)/.*`