@@ -19,22 +19,22 @@ package tests
1919
2020import (
2121 "context"
22+ "errors"
2223 "fmt"
2324 "math/rand"
2425 "os"
25- "testing"
2626
27+ "github.com/apache/iggy/foreign/go/client"
28+ "github.com/apache/iggy/foreign/go/client/tcp"
2729 iggcon "github.com/apache/iggy/foreign/go/contracts"
28- "github.com/apache/iggy/foreign/go/iggycli"
29- "github.com/apache/iggy/foreign/go/tcp"
3030 "github.com/cucumber/godog"
3131)
3232
3333type streamOpsCtxKey struct {}
3434
3535type streamOpsCtx struct {
3636 serverAddr * string
37- client iggycli .Client
37+ client iggcon .Client
3838 streamID * uint32
3939 streamName * string
4040}
@@ -43,7 +43,9 @@ func getStreamOpsCtx(ctx context.Context) *streamOpsCtx {
4343 return ctx .Value (streamOpsCtxKey {}).(* streamOpsCtx )
4444}
4545
46- func streamGivenRunningServer (ctx context.Context ) error {
46+ type streamOpsSteps struct {}
47+
48+ func (s streamOpsSteps ) givenRunningServer (ctx context.Context ) error {
4749 c := getStreamOpsCtx (ctx )
4850 addr := os .Getenv ("IGGY_TCP_ADDRESS" )
4951 if addr == "" {
@@ -53,32 +55,32 @@ func streamGivenRunningServer(ctx context.Context) error {
5355 return nil
5456}
5557
56- func streamGivenAuthenticationAsRoot (ctx context.Context ) error {
58+ func ( s streamOpsSteps ) givenAuthenticationAsRoot (ctx context.Context ) error {
5759 c := getStreamOpsCtx (ctx )
5860 serverAddr := * c .serverAddr
5961
60- client , err := iggycli .NewIggyClient (
61- iggycli .WithTcp (
62+ cli , err := client .NewIggyClient (
63+ client .WithTcp (
6264 tcp .WithServerAddress (serverAddr ),
6365 ),
6466 )
6567 if err != nil {
6668 return fmt .Errorf ("error creating client: %w" , err )
6769 }
6870
69- if err = client .Ping (); err != nil {
71+ if err = cli .Ping (); err != nil {
7072 return fmt .Errorf ("error pinging client: %w" , err )
7173 }
7274
73- if _ , err = client .LoginUser ("iggy" , "iggy" ); err != nil {
75+ if _ , err = cli .LoginUser ("iggy" , "iggy" ); err != nil {
7476 return fmt .Errorf ("error logging in: %v" , err )
7577 }
7678
77- c .client = client
79+ c .client = cli
7880 return nil
7981}
8082
81- func whenCreateStreamWithUniqueName (ctx context.Context ) error {
83+ func ( s streamOpsSteps ) whenCreateStreamWithUniqueName (ctx context.Context ) error {
8284 c := getStreamOpsCtx (ctx )
8385
8486 const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
@@ -98,15 +100,15 @@ func whenCreateStreamWithUniqueName(ctx context.Context) error {
98100 return nil
99101}
100102
101- func thenStreamCreatedSuccessfullyOps (ctx context.Context ) error {
103+ func ( s streamOpsSteps ) thenStreamCreatedSuccessfully (ctx context.Context ) error {
102104 c := getStreamOpsCtx (ctx )
103105 if c .streamID == nil {
104106 return fmt .Errorf ("stream should have been created" )
105107 }
106108 return nil
107109}
108110
109- func thenStreamRetrievableByID (ctx context.Context ) error {
111+ func ( s streamOpsSteps ) thenStreamRetrievableByID (ctx context.Context ) error {
110112 c := getStreamOpsCtx (ctx )
111113 streamIdentifier , _ := iggcon .NewIdentifier (* c .streamID )
112114 stream , err := c .client .GetStream (streamIdentifier )
@@ -119,7 +121,7 @@ func thenStreamRetrievableByID(ctx context.Context) error {
119121 return nil
120122}
121123
122- func thenStreamNameMatchesCreated (ctx context.Context ) error {
124+ func ( s streamOpsSteps ) thenStreamNameMatchesCreated (ctx context.Context ) error {
123125 c := getStreamOpsCtx (ctx )
124126 streamIdentifier , _ := iggcon .NewIdentifier (* c .streamID )
125127 stream , err := c .client .GetStream (streamIdentifier )
@@ -132,41 +134,30 @@ func thenStreamNameMatchesCreated(ctx context.Context) error {
132134 return nil
133135}
134136
135- func initStreamScenarios (sc * godog.ScenarioContext ) {
137+ func initStreamOpsScenario (sc * godog.ScenarioContext ) {
136138 sc .Before (func (ctx context.Context , sc * godog.Scenario ) (context.Context , error ) {
137139 return context .WithValue (context .Background (), streamOpsCtxKey {}, & streamOpsCtx {}), nil
138140 })
139141
140- sc .After (func (ctx context.Context , sc * godog.Scenario , err error ) (context.Context , error ) {
142+ s := & streamOpsSteps {}
143+ sc .Step (`I have a running Iggy server` , s .givenRunningServer )
144+ sc .Step (`I am authenticated as the root user` , s .givenAuthenticationAsRoot )
145+ sc .Step (`I create a stream with a unique name` , s .whenCreateStreamWithUniqueName )
146+ sc .Step (`the stream should be created successfully` , s .thenStreamCreatedSuccessfully )
147+ sc .Step (`the stream should be retrievable by its ID` , s .thenStreamRetrievableByID )
148+ sc .Step (`the stream name should match the created name` , s .thenStreamNameMatchesCreated )
149+
150+ sc .After (func (ctx context.Context , sc * godog.Scenario , scErr error ) (context.Context , error ) {
141151 c := getStreamOpsCtx (ctx )
142152 if c .client != nil && c .streamID != nil {
143153 streamIdentifier , _ := iggcon .NewIdentifier (* c .streamID )
144154 _ = c .client .DeleteStream (streamIdentifier )
145155 }
146- return ctx , nil
156+ if c .client != nil {
157+ if err := c .client .Close (); err != nil {
158+ scErr = errors .Join (scErr , fmt .Errorf ("error closing client: %w" , err ))
159+ }
160+ }
161+ return ctx , scErr
147162 })
148-
149- // Background steps
150- sc .Step (`I have a running Iggy server` , streamGivenRunningServer )
151- sc .Step (`I am authenticated as the root user` , streamGivenAuthenticationAsRoot )
152-
153- // Stream operation steps
154- sc .Step (`I create a stream with a unique name` , whenCreateStreamWithUniqueName )
155- sc .Step (`the stream should be created successfully` , thenStreamCreatedSuccessfullyOps )
156- sc .Step (`the stream should be retrievable by its ID` , thenStreamRetrievableByID )
157- sc .Step (`the stream name should match the created name` , thenStreamNameMatchesCreated )
158- }
159-
160- func TestStreamFeatures (t * testing.T ) {
161- suite := godog.TestSuite {
162- ScenarioInitializer : initStreamScenarios ,
163- Options : & godog.Options {
164- Format : "pretty" ,
165- Paths : []string {"../../scenarios/stream_operations.feature" },
166- TestingT : t ,
167- },
168- }
169- if suite .Run () != 0 {
170- t .Fatal ("failing stream feature tests" )
171- }
172163}
0 commit comments