public void TestFixedTargetAndRateBehaveCorrectly() { var strategy = new LocalizedSamplingStrategy(); strategy.Rules.Add(new Core.Sampling.Local.SamplingRule("name", "test", "get", 10, 0.05)); int count = 0; // fill the fixed target rate Action action = () => { var input = new SamplingInput("name", "test", "get", "", ""); if (strategy.ShouldTrace(input).SampleDecision == SampleDecision.Sampled) { Interlocked.Increment(ref count); } }; var actions = Enumerable.Repeat(action, 10).ToArray(); Parallel.Invoke(actions); Assert.AreEqual(10, count); count = 0; var samplingInput = new SamplingInput("name", "test", "get", "", ""); for (int i = 0; i < 200; i++) { count += strategy.ShouldTrace(samplingInput).SampleDecision == SampleDecision.Sampled ? 1 : 0; } // Default sample rate is 5%. The chance that count == 0 after 200 tries is 0.003%. Assert.IsTrue(count > 0); Assert.IsTrue(count < 50); }
public void TestDefaultRuleWithRequest() { var request = new HttpRequestMessage { RequestUri = new Uri(@"http://www.amazon.com/api/product"), Method = HttpMethod.Post }; request.Headers.Add("Host", "www.amazon.com"); var strategy = new LocalizedSamplingStrategy(); string host = request.Headers.Host; string url = request.RequestUri.AbsolutePath; string method = request.Method.Method; SamplingInput samplingInput = new SamplingInput(host, url, method, "", ""); SamplingResponse samplingResponse = strategy.ShouldTrace(samplingInput); Assert.AreEqual(SampleDecision.Sampled, samplingResponse.SampleDecision); Assert.IsTrue(string.IsNullOrEmpty(samplingResponse.RuleName)); }