| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hsync |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestOnceMoreValue(t *testing.T) { |
| 24 |
c := qt.New(t) |
| 25 |
|
| 26 |
var counter int |
| 27 |
f := func(context.Context) int { |
| 28 |
counter++ |
| 29 |
return counter |
| 30 |
} |
| 31 |
|
| 32 |
omf := OnceMoreValue(f) |
| 33 |
for range 10 { |
| 34 |
c.Assert(omf.Value(context.Background()), qt.Equals, 1) |
| 35 |
} |
| 36 |
omf.Reset() |
| 37 |
for range 10 { |
| 38 |
c.Assert(omf.Value(context.Background()), qt.Equals, 2) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestOnceMoreFunc(t *testing.T) { |
| 43 |
c := qt.New(t) |
| 44 |
|
| 45 |
var counter int |
| 46 |
f := func(context.Context) error { |
| 47 |
counter++ |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
omf := OnceMoreFunc(f) |
| 52 |
for range 10 { |
| 53 |
c.Assert(omf.Do(context.Background()), qt.IsNil) |
| 54 |
c.Assert(counter, qt.Equals, 1) |
| 55 |
} |
| 56 |
omf.Reset() |
| 57 |
for range 10 { |
| 58 |
c.Assert(omf.Do(context.Background()), qt.IsNil) |
| 59 |
c.Assert(counter, qt.Equals, 2) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func BenchmarkOnceMoreValue(b *testing.B) { |
| 64 |
var counter int |
| 65 |
f := func(context.Context) int { |
| 66 |
counter++ |
| 67 |
return counter |
| 68 |
} |
| 69 |
|
| 70 |
for b.Loop() { |
| 71 |
omf := OnceMoreValue(f) |
| 72 |
for range 10 { |
| 73 |
omf.Value(context.Background()) |
| 74 |
} |
| 75 |
omf.Reset() |
| 76 |
for range 10 { |
| 77 |
omf.Value(context.Background()) |
| 78 |
} |
| 79 |
} |
| 80 |
} |