GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit 429430e7 authored by Cecylia Bocovich's avatar Cecylia Bocovich
Browse files

Add a test for cached metrics

parent bbbc0e9d
Pipeline #1591 passed with stage
in 30 minutes and 8 seconds
......@@ -687,5 +687,42 @@ func TestMetrics(t *testing.T) {
ctx.metrics.printMetrics(now)
So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
})
//Test cache
Convey("metrics cache file", func() {
w := httptest.NewRecorder()
data := bytes.NewReader([]byte("test"))
r, err := http.NewRequest("POST", "snowflake.broker/client", data)
r.Header.Set("Snowflake-NAT-TYPE", "restricted")
r.RemoteAddr = "129.97.0.0:1234" //CA geoip
So(err, ShouldBeNil)
clientOffers(ctx, w, r)
cacheFile, err := ioutil.TempFile(os.TempDir(), "cache-file-")
So(err, ShouldBeNil)
defer os.Remove(cacheFile.Name())
now := time.Now()
ctx.metrics.LogStart = now
ctx.metrics.UpdateCountryStats("129.97.0.0", "", NATUnknown)
So(len(ctx.metrics.CountryStats.NatUnknown), ShouldEqual, 1)
So(ctx.metrics.CountryStats.NatUnknown["129.97.0.0"], ShouldEqual, true)
So(ctx.metrics.CountryStats.Counts["CA"], ShouldEqual, 1)
So(ctx.metrics.ClientRestrictedDeniedCount, ShouldEqual, 1)
err = ctx.metrics.WriteCache(cacheFile.Name())
So(err, ShouldBeNil)
ctx.metrics.zeroMetrics()
So(len(ctx.metrics.CountryStats.NatUnknown), ShouldEqual, 0)
So(ctx.metrics.LogStart, ShouldNotEqual, now) // time has passed since previous LogStart value
err = ctx.metrics.ReadCache(cacheFile.Name())
So(len(ctx.metrics.CountryStats.NatUnknown), ShouldEqual, 1)
So(ctx.metrics.CountryStats.NatUnknown["129.97.0.0"], ShouldEqual, true)
So(ctx.metrics.CountryStats.Counts["CA"], ShouldEqual, 1)
So(ctx.metrics.ClientRestrictedDeniedCount, ShouldEqual, 1)
So(ctx.metrics.LogStart, ShouldEqual, now)
})
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment