diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..2e5ebcb --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,32 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: ['1.18', '1.19', '1.20', '1.21.x'] + + steps: + - uses: actions/checkout@v4 + + - name: Setup Go ${{ matrix.go-version }} + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/round_robin.go b/round_robin.go index d2bff3c..ab83d2c 100644 --- a/round_robin.go +++ b/round_robin.go @@ -30,9 +30,8 @@ func (r *RoundRobin[O]) Next() *O { n := atomic.AddUint32(&r.next, 1) if int(n) > len(r.objects) { - atomic.StoreUint32(&r.next, 0) + atomic.StoreUint32(&r.next, 1) n = 1 } return r.objects[(int(n)-1)%len(r.objects)] } - diff --git a/round_robin_test.go b/round_robin_test.go index 1587266..9c7b9d0 100644 --- a/round_robin_test.go +++ b/round_robin_test.go @@ -39,6 +39,22 @@ func TestRoundRobin(t *testing.T) { {6, "resource-6"}, {7, "resource-7"}, {1, "resource-1"}, + {2, "resource-2"}, + {3, "resource-3"}, + {4, "resource-4"}, + {5, "resource-5"}, + {6, "resource-6"}, + {7, "resource-7"}, + {1, "resource-1"}, + {2, "resource-2"}, + {3, "resource-3"}, + {4, "resource-4"}, + {5, "resource-5"}, + {6, "resource-6"}, + {7, "resource-7"}, + {1, "resource-1"}, + {2, "resource-2"}, + {3, "resource-3"}, }, }, { @@ -50,18 +66,15 @@ func TestRoundRobin(t *testing.T) { for i, test := range tests { rr, err := New(test.resources...) - - if got, want := !(err == nil), test.iserr; got != want { - t.Errorf("tests[%d] - RoundRobin iserr is wrong. want: %v, but got: %v", i, test.want, got) + if err != nil && !test.iserr { + t.Errorf("tests[%d] - RoundRobin iserr is wrong. want: %v, but got: %v", i, test.want, true) } - gots := make([]*resource, 0, len(test.want)) for j := 0; j < len(test.want); j++ { - gots = append(gots, rr.Next()) - } - - if got, want := gots, test.want; !reflect.DeepEqual(got, want) { - t.Errorf("tests[%d] - RoundRobin is wrong. want: %v, got: %v", i, want, got) + got := rr.Next() + if !reflect.DeepEqual(got, test.want[j]) { + t.Errorf("tests[%d] index[%d] - RoundRobin is wrong. want: %v, got: %v", i, j, *test.want[j], *got) + } } } }