Repository: nats-io/nats.go
Pull request #1278 (2023-05-30 12:51:58)

[CHANGED] Set default ack policy to explicit


Patch:
diff --git a/jetstream/consumer_config.go b/jetstream/consumer_config.go
index 713daf8..750e83c 100644
--- a/jetstream/consumer_config.go
+++ b/jetstream/consumer_config.go
@@ -190,18 +190,15 @@ func (p DeliverPolicy) String() string {
 }
 
 const (
-	// AckNonePolicy requires no acks for delivered messages.
-	AckNonePolicy AckPolicy = iota
+	// AckExplicitPolicy requires ack or nack for all messages.
+	AckExplicitPolicy AckPolicy = iota
 
 	// AckAllPolicy when acking a sequence number, this implicitly acks all
 	// sequences below this one as well.
 	AckAllPolicy
 
-	// AckExplicitPolicy requires ack or nack for all messages.
-	AckExplicitPolicy
-
-	// For configuration mismatch check
-	ackPolicyNotSet = 99
+	// AckNonePolicy requires no acks for delivered messages.
+	AckNonePolicy
 )
 
 func (p *AckPolicy) UnmarshalJSON(data []byte) error {
@@ -227,7 +224,7 @@ func (p AckPolicy) MarshalJSON() ([]byte, error) {
 	case AckExplicitPolicy:
 		return json.Marshal("explicit")
 	}
-	return nil, fmt.Errorf("nats: unknown acknowlegement policy %v", p)
+	return nil, fmt.Errorf("nats: unknown acknowledgement policy %v", p)
 }
 
 func (p AckPolicy) String() string {
@@ -238,8 +235,6 @@ func (p AckPolicy) String() string {
 		return "AckAll"
 	case AckExplicitPolicy:
 		return "AckExplicit"
-	case ackPolicyNotSet:
-		return "Not Initialized"
 	}
 	return "Unknown AckPolicy"
 }


Test patch:
diff --git a/jetstream/test/stream_test.go b/jetstream/test/stream_test.go
index e94a941..3543775 100644
--- a/jetstream/test/stream_test.go
+++ b/jetstream/test/stream_test.go
@@ -35,27 +35,27 @@ func TestAddConsumer(t *testing.T) {
 	}{
 		{
 			name:           "create durable pull consumer",
-			consumerConfig: jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckExplicitPolicy},
+			consumerConfig: jetstream.ConsumerConfig{Durable: "dur"},
 			shouldCreate:   true,
 		},
 		{
 			name:           "create ephemeral pull consumer",
-			consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy},
+			consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckNonePolicy},
 			shouldCreate:   true,
 		},
 		{
 			name:           "with filter subject",
-			consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy, FilterSubject: "FOO.A"},
+			consumerConfig: jetstream.ConsumerConfig{FilterSubject: "FOO.A"},
 			shouldCreate:   true,
 		},
 		{
 			name:           "with multiple filter subjects",
-			consumerConfig: jetstream.ConsumerConfig{AckPolicy: jetstream.AckExplicitPolicy, FilterSubjects: []string{"FOO.A", "FOO.B"}},
+			consumerConfig: jetstream.ConsumerConfig{FilterSubjects: []string{"FOO.A", "FOO.B"}},
 			shouldCreate:   true,
 		},
 		{
 			name:           "consumer already exists, update",
-			consumerConfig: jetstream.ConsumerConfig{Durable: "dur", AckPolicy: jetstream.AckExplicitPolicy, Description: "test consumer"},
+			consumerConfig: jetstream.ConsumerConfig{Durable: "dur", Description: "test consumer"},
 		},
 		{
 			name:           "consumer already exists, illegal update",
@@ -64,7 +64,7 @@ func TestAddConsumer(t *testing.T) {
 		},
 		{
 			name:           "invalid durable name",
-			consumerConfig: jetstream.ConsumerConfig{Durable: "dur.123", AckPolicy: jetstream.AckExplicitPolicy},
+			consumerConfig: jetstream.ConsumerConfig{Durable: "dur.123"},
 			withError:      jetstream.ErrInvalidConsumerName,
 		},
 	}
@@ -112,10 +112,13 @@ func TestAddConsumer(t *testing.T) {
 					t.Fatalf("Expected request on %s; got %s", sub.Subject, err)
 				}
 			}
-			_, err = s.Consumer(ctx, c.CachedInfo().Name)
+			ci, err := s.Consumer(ctx, c.CachedInfo().Name)
 			if err != nil {
 				t.Fatalf("Unexpected error: %v", err)
 			}
+			if ci.CachedInfo().Config.AckPolicy != test.consumerConfig.AckPolicy {
+				t.Fatalf("Invalid ack policy; want: %s; got: %s", test.consumerConfig.AckPolicy, ci.CachedInfo().Config.AckPolicy)
+			}
 		})
 	}
 }