| 137 | } |
| 138 | |
| 139 | func TestLevelSampler(t *testing.T) { |
| 140 | // Create mock samplers that return true for specific levels |
| 141 | traceSampler := &BasicSampler{N: 1} // Always sample |
| 142 | debugSampler := &BasicSampler{N: 0} // Never sample |
| 143 | infoSampler := &BasicSampler{N: 1} // Always sample |
| 144 | warnSampler := &BasicSampler{N: 0} // Never sample |
| 145 | errorSampler := &BasicSampler{N: 1} // Always sample |
| 146 | |
| 147 | sampler := LevelSampler{ |
| 148 | TraceSampler: traceSampler, |
| 149 | DebugSampler: debugSampler, |
| 150 | InfoSampler: infoSampler, |
| 151 | WarnSampler: warnSampler, |
| 152 | ErrorSampler: errorSampler, |
| 153 | } |
| 154 | |
| 155 | // Test each level |
| 156 | if !sampler.Sample(TraceLevel) { |
| 157 | t.Error("TraceLevel should be sampled") |
| 158 | } |
| 159 | if sampler.Sample(DebugLevel) { |
| 160 | t.Error("DebugLevel should not be sampled") |
| 161 | } |
| 162 | if !sampler.Sample(InfoLevel) { |
| 163 | t.Error("InfoLevel should be sampled") |
| 164 | } |
| 165 | if sampler.Sample(WarnLevel) { |
| 166 | t.Error("WarnLevel should not be sampled") |
| 167 | } |
| 168 | if !sampler.Sample(ErrorLevel) { |
| 169 | t.Error("ErrorLevel should be sampled") |
| 170 | } |
| 171 | |
| 172 | // Test levels not covered by the LevelSampler sampler (FatalLevel, PanicLevel, NoLevel) - should return true |
| 173 | if !sampler.Sample(FatalLevel) { |
| 174 | t.Error("FatalLevel should return true when no sampler is set") |
| 175 | } |
| 176 | if !sampler.Sample(PanicLevel) { |
| 177 | t.Error("PanicLevel should return true when no sampler is set") |
| 178 | } |
| 179 | if !sampler.Sample(NoLevel) { |
| 180 | t.Error("NoLevel should return true when no sampler is set") |
| 181 | } |
| 182 | } |