Repository: nats-io/nats.go (go)

Issue:
API Design nak with delay non testable with mocks
## Feature Request

In the new jetstream API there is a change where the `NakWithDelay`  was replaced by the `Nak(jetstream.WithNakDelay(...))`

Prior to the update I was mocking a nats.Message with a custom interface and I was able to check if a nak occurred with a given duration.

Now I can't because the `NakWithDelay` is now a functional parameter option of the `Nak` function and all is private so I can't check what options I pass to the nak.

Is this something that could be changed ? 

#### Use Case:

Testing that a nak with delay contains a certain duration 

#### Proposed Change:

Maybe exposing the ackOpts and his fields ?

#### Who Benefits From The Change(s)?

Every developers that mocks jetstream.Message and has delay with nak





Pull request description:
[CHANGED] Move NakWithDelay to separate method
This cleans up the API a bit, avoiding having a method with a single option to modify the behavior.

Resolves #1299 

Patch:
diff --git a/jetstream/message.go b/jetstream/message.go
index 6862192..36216c5 100644
--- a/jetstream/message.go
+++ b/jetstream/message.go
@@ -49,7 +49,11 @@ type (
 		DoubleAck(context.Context) error
 		// Nak negatively acknowledges a message
 		// This tells the server to redeliver the message
-		Nak(...NakOpt) error
+		Nak() error
+		// NakWithDelay negatively acknowledges a message
+		// This tells the server to redeliver the message
+		// after the given `delay` duration
+		NakWithDelay(delay time.Duration) error
 		// InProgress tells the server that this message is being worked on
 		// It resets the redelivery timer on the server
 		InProgress() error
@@ -85,10 +89,6 @@ type (
 		nakDelay time.Duration
 	}
 
-	AckOpt func(*ackOpts) error
-
-	NakOpt func(*ackOpts) error
-
 	ackType []byte
 )
 
@@ -189,16 +189,14 @@ func (m *jetStreamMsg) DoubleAck(ctx context.Context) error {
 
 // Nak negatively acknowledges a message
 // This tells the server to redeliver the message
-// Nak() can be supplied with following options:
-// - WithNakDelay() - specify the duration after which the mesage should be redelivered
-func (m *jetStreamMsg) Nak(opts ...NakOpt) error {
-	var o ackOpts
-	for _, opt := range opts {
-		if err := opt(&o); err != nil {
-			return err
-		}
-	}
-	return m.ackReply(context.Background(), ackNak, false, o)
+func (m *jetStreamMsg) Nak() error {
+	return m.ackReply(context.Background(), ackNak, false, ackOpts{})
+}
+
+// NakWithDelay negatively acknowledges a message
+// This tells the server to redeliver the message after the given `delay` duration
+func (m *jetStreamMsg) NakWithDelay(delay time.Duration) error {
+	return m.ackReply(context.Background(), ackNak, false, ackOpts{nakDelay: delay})
 }
 
 // InProgress tells the server that this message is being worked on
diff --git a/jetstream/options.go b/jetstream/options.go
index 51e72ab..7b8f1fe 100644
--- a/jetstream/options.go
+++ b/jetstream/options.go
@@ -242,14 +242,6 @@ func WithSubjectFilter(subject string) StreamInfoOpt {
 	}
 }
 
-// WithNakDelay can be used to specify the duration after which the message should be redelivered
-func WithNakDelay(delay time.Duration) NakOpt {
-	return func(opts *ackOpts) error {
-		opts.nakDelay = delay
-		return nil
-	}
-}
-
 // WithMsgID sets the message ID used for deduplication.
 func WithMsgID(id string) PublishOpt {
 	return func(opts *pubOpts) error {


Test patch:
diff --git a/jetstream/test/message_test.go b/jetstream/test/message_test.go
index 99cb129..fc9efc3 100644
--- a/jetstream/test/message_test.go
+++ b/jetstream/test/message_test.go
@@ -306,7 +306,7 @@ func TestAckVariants(t *testing.T) {
 			t.Fatalf("Unexpected error: %v", err)
 		}
 
-		if err := msg.Nak(jetstream.WithNakDelay(123 * time.Nanosecond)); err != nil {
+		if err := msg.NakWithDelay(123 * time.Nanosecond); err != nil {
 			t.Fatalf("Unexpected error: %v", err)
 		}
 		ack, err := sub.NextMsgWithContext(ctx)