|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import unittest |
| 19 | + |
| 20 | +from skywalking.sampling.sampling_service import SamplingService, SamplingServiceAsync |
| 21 | + |
| 22 | + |
| 23 | +class TestSampling(unittest.TestCase): |
| 24 | + |
| 25 | + def test_try_sampling(self): |
| 26 | + from skywalking import config |
| 27 | + |
| 28 | + config.sample_n_per_3_secs = 2 |
| 29 | + sampling_service = SamplingService() |
| 30 | + assert sampling_service.try_sampling() |
| 31 | + assert sampling_service.try_sampling() |
| 32 | + assert not sampling_service.try_sampling() |
| 33 | + |
| 34 | + def test_force_sampled(self): |
| 35 | + |
| 36 | + from skywalking import config |
| 37 | + |
| 38 | + config.sample_n_per_3_secs = 1 |
| 39 | + sampling_service = SamplingService() |
| 40 | + assert sampling_service.try_sampling() |
| 41 | + sampling_service.force_sampled() |
| 42 | + assert sampling_service.sampling_factor == 2 |
| 43 | + |
| 44 | + def test_reset_sampling_factor(self): |
| 45 | + from skywalking import config |
| 46 | + |
| 47 | + config.sample_n_per_3_secs = 1 |
| 48 | + sampling_service = SamplingService() |
| 49 | + assert sampling_service.try_sampling() |
| 50 | + assert not sampling_service.try_sampling() |
| 51 | + sampling_service.reset_sampling_factor() |
| 52 | + assert sampling_service.try_sampling() |
| 53 | + |
| 54 | + |
| 55 | +class TestSamplingAsync(unittest.IsolatedAsyncioTestCase): |
| 56 | + |
| 57 | + async def test_try_sampling(self): |
| 58 | + from skywalking import config |
| 59 | + |
| 60 | + config.sample_n_per_3_secs = 2 |
| 61 | + sampling_service = SamplingServiceAsync() |
| 62 | + assert sampling_service.try_sampling() |
| 63 | + assert sampling_service.try_sampling() |
| 64 | + assert not sampling_service.try_sampling() |
| 65 | + |
| 66 | + async def test_force_sampled(self): |
| 67 | + |
| 68 | + from skywalking import config |
| 69 | + |
| 70 | + config.sample_n_per_3_secs = 1 |
| 71 | + sampling_service = SamplingServiceAsync() |
| 72 | + assert sampling_service.try_sampling() |
| 73 | + sampling_service.force_sampled() |
| 74 | + assert sampling_service.sampling_factor == 2 |
| 75 | + |
| 76 | + async def test_reset_sampling_factor(self): |
| 77 | + from skywalking import config |
| 78 | + |
| 79 | + config.sample_n_per_3_secs = 1 |
| 80 | + sampling_service = SamplingServiceAsync() |
| 81 | + assert sampling_service.try_sampling() |
| 82 | + assert not sampling_service.try_sampling() |
| 83 | + await sampling_service.reset_sampling_factor() |
| 84 | + assert sampling_service.try_sampling() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + unittest.main() |
0 commit comments