Skip to content

Commit

Permalink
Merge pull request #4 from superbear/main
Browse files Browse the repository at this point in the history
fix issues/3: Starting from the second round, the first object will be fetched twice
  • Loading branch information
thegeekyasian authored Jun 5, 2024
2 parents ee0e2ad + f7e66d4 commit 019657e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
3 changes: 1 addition & 2 deletions round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}

31 changes: 22 additions & 9 deletions round_robin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
{
Expand All @@ -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)
}
}
}
}
Expand Down

0 comments on commit 019657e

Please sign in to comment.