Skip to content

Commit

Permalink
sub,client: WIP on mocking liftbridge client functions
Browse files Browse the repository at this point in the history
Signed-off-by: Simarpreet Singh <[email protected]>
  • Loading branch information
simar7 committed Aug 28, 2018
1 parent a5bfa02 commit 28fcd3f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
26 changes: 26 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"context"
"fmt"
"testing"

"github.com/liftbridge-io/go-liftbridge"
"github.com/stretchr/testify/assert"
)

func (mlbc MockLiftBridgeClient) CreateStream(ctx context.Context, stream liftbridge.StreamInfo) error {
fmt.Println("fake CreateStream() called")
return nil
}

func Test_Client_CreateStream(t *testing.T) {
mlbc := MockLiftBridgeClient{
streamInfo: liftbridge.StreamInfo{
Subject: "foo",
Name: "foo-stream",
ReplicationFactor: 3,
},
}
assert.NoError(t, createStream(mlbc, context.Background(), mlbc.streamInfo))
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"sync"

liftbridge "github.com/liftbridge-io/go-liftbridge"
Expand All @@ -9,12 +10,14 @@ import (
type EventStreamClient interface {
Pub()
Sub()
CreateStream(context.Context, liftbridge.StreamInfo) error
}

type LiftBridgeClient struct {
wg *sync.WaitGroup
servers []string
streamInfo liftbridge.StreamInfo
client liftbridge.Client
}

func main() {
Expand Down
4 changes: 3 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"sync"
"testing"

liftbridge "github.com/liftbridge-io/go-liftbridge"
"github.com/stretchr/testify/assert"
)

type MockLiftBridgeClient struct {
wg *sync.WaitGroup
wg *sync.WaitGroup
streamInfo liftbridge.StreamInfo
}

func (mlbc MockLiftBridgeClient) Pub() {
Expand Down
18 changes: 14 additions & 4 deletions sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
lift "github.com/liftbridge-io/go-liftbridge/liftbridge-grpc"
)

func sub(lbcIface EventStreamClient) {
lbcIface.Sub()
}

func (lbc LiftBridgeClient) Sub() {
defer lbc.wg.Done()
fmt.Println("real Sub() called")
Expand All @@ -17,9 +21,10 @@ func (lbc LiftBridgeClient) Sub() {
if err != nil {
panic(err)
}
defer client.Close()
lbc.client = client
defer lbc.client.Close()

if err := client.CreateStream(context.Background(), lbc.streamInfo); err != nil {
if err := lbc.client.CreateStream(context.Background(), lbc.streamInfo); err != nil {
if err != liftbridge.ErrStreamExists {
panic(err)
}
Expand All @@ -38,6 +43,11 @@ func (lbc LiftBridgeClient) Sub() {
<-ctx.Done()
}

func sub(lbcIface EventStreamClient) {
lbcIface.Sub()
func createStream(lbcIface EventStreamClient, ctx context.Context, stream liftbridge.StreamInfo) error {
return lbcIface.CreateStream(ctx, stream)
}

func (lbc LiftBridgeClient) CreateStream(ctx context.Context, stream liftbridge.StreamInfo) error {
fmt.Println("real createStream() called")
return nil
}

0 comments on commit 28fcd3f

Please sign in to comment.