summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/tests/Jaeger/Sampler/ProbablisticSamplerTest.php
blob: 2840ebf8c86f3993f79e9e6f4807d3bc919c1556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace Jaeger\Tests\Sampler;

use Jaeger\Sampler\ProbabilisticSampler;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use const Jaeger\SAMPLER_PARAM_TAG_KEY;
use const Jaeger\SAMPLER_TYPE_PROBABILISTIC;
use const Jaeger\SAMPLER_TYPE_TAG_KEY;

class ProbablisticSamplerTest extends TestCase
{
    /**
     * @test
     * @dataProvider samplerProvider
     * @param float $rate
     * @param mixed $traceId
     * @param bool $decision
     */
    public function shouldDetermineWhetherOrTraceShouldBeSampled($rate, $traceId, $decision)
    {
        $sampler = new ProbabilisticSampler($rate);

        list($sampled, $tags) = $sampler->isSampled($traceId);

        $this->assertEquals($decision, $sampled);
        $this->assertEquals([
            SAMPLER_TYPE_TAG_KEY  => SAMPLER_TYPE_PROBABILISTIC,
            SAMPLER_PARAM_TAG_KEY => $rate,
        ], $tags);

        $sampler->close();
    }

    public function samplerProvider()
    {
        return [
            [1.0, PHP_INT_MAX-1, true],
            [0, 0, false],
            [0.5, PHP_INT_MIN + 10, true],
            [0.5, PHP_INT_MAX - 10, false],
        ];
    }

    /**
     * @test
     * @dataProvider rateProvider
     * @param mixed $rate
     */
    public function shouldThrowOutOfBoundsExceptionInCaseOfInvalidRate($rate)
    {
        $this->expectException(OutOfBoundsException::class);
        $this->expectExceptionMessage('Sampling rate must be between 0.0 and 1.0.');

        new ProbabilisticSampler($rate);
    }

    public function rateProvider()
    {
        return [
            [1.1],
            [-0.1],
            [PHP_INT_MAX],
            [PHP_INT_MIN],
        ];
    }
}