1+ /**
2+ * Copyright 2018 Google Inc. All Rights Reserved.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import * as assert from 'assert' ;
18+ import * as mocha from 'mocha' ;
19+
20+ import { SamplerImpl } from '../src/trace/config/sampler' ;
21+ import { RootSpanImpl } from '../src/trace/model/rootspan' ;
22+ import { TracerImpl } from '../src/trace/model/tracer' ;
23+
24+ const tracer = new TracerImpl ( ) ;
25+
26+ describe ( 'Sampler' , ( ) => {
27+ /**
28+ * Should create a sampler
29+ */
30+ describe ( 'new Sampler()' , ( ) => {
31+ it ( 'should create a Sampler instance' , ( ) => {
32+ const sampler = new SamplerImpl ( ) ;
33+ assert . ok ( sampler instanceof SamplerImpl ) ;
34+ } ) ;
35+ } ) ;
36+
37+ /**
38+ * Should create a sampler with traceId
39+ */
40+ describe ( 'new Sampler(traceId)' , ( ) => {
41+ it ( 'should create a Sampler instance' , ( ) => {
42+ const root = new RootSpanImpl ( tracer ) ;
43+ const sampler = new SamplerImpl ( root . traceId ) ;
44+ assert . ok ( sampler instanceof SamplerImpl ) ;
45+ } ) ;
46+ } ) ;
47+
48+ /**
49+ * Should return the SamplerImpl
50+ */
51+ describe ( 'always()' , ( ) => {
52+ it ( 'should return a sampler instance' , ( ) => {
53+ const sampler = new SamplerImpl ( ) ;
54+ const samplerAlways = sampler . always ( ) ;
55+ assert . ok ( samplerAlways instanceof SamplerImpl ) ;
56+ } ) ;
57+ } ) ;
58+
59+ /**
60+ * Should return the SamplerImpl
61+ */
62+ describe ( 'never()' , ( ) => {
63+ it ( 'should return a sampler instance' , ( ) => {
64+ const sampler = new SamplerImpl ( ) ;
65+ const samplerNever = sampler . never ( ) ;
66+ assert . ok ( samplerNever instanceof SamplerImpl ) ;
67+ } ) ;
68+ } ) ;
69+
70+ /**
71+ * Should return the SamplerImpl
72+ */
73+ describe ( 'probability()' , ( ) => {
74+ it ( 'should return a sampler instance' , ( ) => {
75+ const PROBABILITY = 0.5 ;
76+ const sampler = new SamplerImpl ( ) ;
77+ const samplerProbability = sampler . probability ( PROBABILITY ) ;
78+ assert . ok ( samplerProbability instanceof SamplerImpl ) ;
79+ } ) ;
80+ } ) ;
81+
82+ /**
83+ * Should return true
84+ */
85+ describe ( 'shouldSample() always' , ( ) => {
86+ it ( 'should return true' , ( ) => {
87+ const root = new RootSpanImpl ( tracer ) ;
88+ const sampler = new SamplerImpl ( ) ;
89+ sampler . always ( ) ;
90+ const samplerShouldSampler = sampler . shouldSample ( root . traceId ) ;
91+ assert . ok ( samplerShouldSampler ) ;
92+ } ) ;
93+ } ) ;
94+ /**
95+ * Should return false
96+ */
97+ describe ( 'shouldSample() never' , ( ) => {
98+ it ( 'should return false' , ( ) => {
99+ const root = new RootSpanImpl ( tracer ) ;
100+ const sampler = new SamplerImpl ( ) ;
101+ sampler . never ( ) ;
102+ const samplerShouldSampler = sampler . shouldSample ( root . traceId ) ;
103+ assert . ok ( ! samplerShouldSampler ) ;
104+ } ) ;
105+ } ) ;
106+ } ) ;
0 commit comments