Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit e94e0c4

Browse files
eldreygalindosilva-fabio
authored andcommitted
test: add sampler test
1 parent f40be38 commit e94e0c4

2 files changed

Lines changed: 110 additions & 7 deletions

File tree

packages/opencensus-core/src/trace/sampler/sampler.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {debug, randomSpanId} from '../../internal/util';
18-
import {Sampler} from './types'
18+
import {Sampler} from './types';
1919

2020
const MIN_NUMBER = 1e-4;
2121
const MAX_NUMBER = 0xffffffffffffffff;
@@ -28,7 +28,7 @@ export class SamplerImpl {
2828
/**
2929
* Constructs a new SamplerImpl instance.
3030
* @param traceId Used for probability calculation
31-
*/
31+
*/
3232
constructor(traceId?: string) {
3333
if (traceId) {
3434
this.traceId = traceId;
@@ -63,10 +63,8 @@ export class SamplerImpl {
6363
probability(probability?: number): Sampler {
6464
if (probability == null || probability > MAX_NUMBER) {
6565
return this.always();
66-
}
67-
else if (probability < MIN_NUMBER) {
66+
} else if (probability < MIN_NUMBER) {
6867
return this.never();
69-
7068
}
7169
this.idUpperBound = probability * MAX_NUMBER;
7270
return this;
@@ -76,7 +74,7 @@ export class SamplerImpl {
7674
* Checks if trace belong the sample.
7775
* @param traceId Used to check the probability
7876
* @returns a boolean. True if the traceId is in probability
79-
* False if the traceId is not in probability.
77+
* False if the traceId is not in probability.
8078
*/
8179
shouldSample(traceId: string): boolean {
8280
const LOWER_BYTES = traceId.substring(16);
@@ -89,5 +87,4 @@ export class SamplerImpl {
8987
return false;
9088
}
9189
}
92-
9390
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)