Skip to content

Commit

Permalink
Add check and set function
Browse files Browse the repository at this point in the history
  • Loading branch information
adibrastegarnia committed Mar 27, 2020
1 parent e2ecd2d commit 80134f1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pkg/client/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package counter

import (
"context"

api "github.com/atomix/api/proto/atomix/counter"
"github.com/atomix/api/proto/atomix/headers"
"github.com/atomix/go-client/pkg/client/primitive"
Expand Down Expand Up @@ -47,6 +48,9 @@ type Counter interface {

// Decrement decrements the counter by the given delta
Decrement(ctx context.Context, delta int64) (int64, error)

// CAS checks the counter value and then updates its current value
CAS(ctx context.Context, expect int64, update int64) (bool, error)
}

// New creates a new counter for the given partitions
Expand Down Expand Up @@ -149,6 +153,26 @@ func (c *counter) Decrement(ctx context.Context, delta int64) (int64, error) {
return response.(*api.DecrementResponse).NextValue, nil
}

func (c *counter) CAS(ctx context.Context, expect int64, update int64) (bool, error) {
response, err := c.instance.DoCommand(ctx, func(ctx context.Context, conn *grpc.ClientConn, header *headers.RequestHeader) (*headers.ResponseHeader, interface{}, error) {
client := api.NewCounterServiceClient(conn)
request := &api.CheckAndSetRequest{
Header: header,
Expect: expect,
Update: update,
}
response, err := client.CheckAndSet(ctx, request)
if err != nil {
return nil, nil, err
}
return response.Header, response, nil
})
if err != nil {
return false, err
}
return response.(*api.CheckAndSetResponse).Succeeded, nil
}

func (c *counter) Close(ctx context.Context) error {
return c.instance.Close(ctx)
}
Expand Down
17 changes: 15 additions & 2 deletions pkg/client/counter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package counter

import (
"context"
"testing"

"github.com/atomix/go-client/pkg/client/primitive"
"github.com/atomix/go-client/pkg/client/test"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCounterOperations(t *testing.T) {
Expand Down Expand Up @@ -69,6 +70,18 @@ func TestCounterOperations(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(10), value)

casValue, err := counter.CAS(context.TODO(), 15, 25)
assert.NoError(t, err)
assert.Equal(t, false, casValue)

casValue, err = counter.CAS(context.TODO(), 10, 20)
assert.NoError(t, err)
assert.Equal(t, true, casValue)

value, err = counter.Get(context.TODO())
assert.NoError(t, err)
assert.Equal(t, int64(20), value)

err = counter.Close(context.Background())
assert.NoError(t, err)

Expand All @@ -80,7 +93,7 @@ func TestCounterOperations(t *testing.T) {

value, err = counter1.Get(context.TODO())
assert.NoError(t, err)
assert.Equal(t, int64(10), value)
assert.Equal(t, int64(20), value)

err = counter1.Close(context.Background())
assert.NoError(t, err)
Expand Down

0 comments on commit 80134f1

Please sign in to comment.