@@ -21,8 +21,13 @@ import {
21
21
assertRejects ,
22
22
assertStringIncludes ,
23
23
} from "jsr:@std/assert" ;
24
- import { DeliverPolicy , jetstream , jetstreamManager } from "../mod.ts" ;
25
- import type { JsMsg } from "../mod.ts" ;
24
+ import {
25
+ ConsumerDebugEvents ,
26
+ DeliverPolicy ,
27
+ jetstream ,
28
+ jetstreamManager ,
29
+ } from "../mod.ts" ;
30
+ import type { ConsumerMessages , JsMsg } from "../mod.ts" ;
26
31
import type {
27
32
OrderedConsumerMessages ,
28
33
OrderedPullConsumerImpl ,
@@ -94,87 +99,6 @@ Deno.test("ordered consumers - fetch", async () => {
94
99
await cleanup ( ns , nc ) ;
95
100
} ) ;
96
101
97
- Deno . test ( "ordered consumers - fetch reset" , async ( ) => {
98
- const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
99
- const js = jetstream ( nc ) ;
100
-
101
- const jsm = await jetstreamManager ( nc ) ;
102
- await jsm . streams . add ( { name : "test" , subjects : [ "test.*" ] } ) ;
103
- await js . publish ( "test.a" ) ;
104
- await js . publish ( "test.b" ) ;
105
- await js . publish ( "test.c" ) ;
106
-
107
- const oc = await js . consumers . get ( "test" ) as OrderedPullConsumerImpl ;
108
- assertExists ( oc ) ;
109
-
110
- const seen : number [ ] = new Array ( 3 ) . fill ( 0 ) ;
111
- let done = deferred ( ) ;
112
-
113
- const callback = ( m : JsMsg ) => {
114
- const idx = m . seq - 1 ;
115
- seen [ idx ] ++ ;
116
- // mess with the internals so we see these again
117
- if ( seen [ idx ] === 1 ) {
118
- oc . cursor . deliver_seq -- ;
119
- oc . cursor . stream_seq -- ;
120
- }
121
- iter . stop ( ) ;
122
- done . resolve ( ) ;
123
- } ;
124
-
125
- let iter = await oc . fetch ( {
126
- max_messages : 1 ,
127
- //@ts -ignore: callback not exposed
128
- callback,
129
- } ) ;
130
- await done ;
131
- done = deferred ( ) ;
132
-
133
- iter = await oc . fetch ( {
134
- max_messages : 1 ,
135
- //@ts -ignore: callback not exposed
136
- callback,
137
- } ) ;
138
- await done ;
139
- done = deferred ( ) ;
140
-
141
- iter = await oc . fetch ( {
142
- max_messages : 1 ,
143
- //@ts -ignore: callback not exposed
144
- callback,
145
- } ) ;
146
- await done ;
147
- done = deferred ( ) ;
148
-
149
- iter = await oc . fetch ( {
150
- max_messages : 1 ,
151
- //@ts -ignore: callback not exposed
152
- callback,
153
- } ) ;
154
- await done ;
155
- done = deferred ( ) ;
156
-
157
- iter = await oc . fetch ( {
158
- max_messages : 1 ,
159
- //@ts -ignore: callback not exposed
160
- callback,
161
- } ) ;
162
- await done ;
163
- done = deferred ( ) ;
164
-
165
- iter = await oc . fetch ( {
166
- max_messages : 1 ,
167
- //@ts -ignore: callback not exposed
168
- callback,
169
- } ) ;
170
- await done ;
171
-
172
- assertEquals ( seen , [ 2 , 2 , 2 ] ) ;
173
- assertEquals ( oc . serial , 6 ) ;
174
-
175
- await cleanup ( ns , nc ) ;
176
- } ) ;
177
-
178
102
Deno . test ( "ordered consumers - consume reset" , async ( ) => {
179
103
const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
180
104
const js = jetstream ( nc ) ;
@@ -985,3 +909,153 @@ Deno.test("ordered consumers - name prefix", async () => {
985
909
986
910
await cleanup ( ns , nc ) ;
987
911
} ) ;
912
+
913
+ Deno . test ( "ordered consumers - fetch reset" , async ( ) => {
914
+ const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
915
+ const jsm = await jetstreamManager ( nc ) ;
916
+
917
+ await jsm . streams . add ( { name : "A" , subjects : [ "a" ] } ) ;
918
+ const js = jetstream ( nc ) ;
919
+ await js . publish ( "a" , JSON . stringify ( 1 ) ) ;
920
+
921
+ const c = await js . consumers . get ( "A" ) as OrderedPullConsumerImpl ;
922
+
923
+ let resets = 0 ;
924
+ function countResets ( iter : ConsumerMessages ) : Promise < void > {
925
+ return ( async ( ) => {
926
+ for await ( const s of await iter . status ( ) ) {
927
+ if ( s . type === ConsumerDebugEvents . Reset ) {
928
+ resets ++ ;
929
+ }
930
+ }
931
+ } ) ( ) ;
932
+ }
933
+
934
+ // after the first message others will get published
935
+ let iter = await c . fetch ( { max_messages : 10 , expires : 3_000 } ) ;
936
+ const first = countResets ( iter ) ;
937
+ const sequences = [ ] ;
938
+ for await ( const m of iter ) {
939
+ sequences . push ( m . json ( ) ) ;
940
+ // mess with the internal state to cause a reset
941
+ if ( m . seq === 1 ) {
942
+ c . cursor . deliver_seq = 3 ;
943
+ const buf = [ ] ;
944
+ for ( let i = 2 ; i < 20 ; i ++ ) {
945
+ buf . push ( js . publish ( "a" , JSON . stringify ( i ) ) ) ;
946
+ }
947
+ await Promise . all ( buf ) ;
948
+ }
949
+ }
950
+
951
+ iter = await c . fetch ( { max_messages : 10 , expires : 2_000 } ) ;
952
+ const second = countResets ( iter ) ;
953
+
954
+ const done = ( async ( ) => {
955
+ for await ( const m of iter ) {
956
+ sequences . push ( m . json ( ) ) ;
957
+ }
958
+ } ) ( ) . catch ( ) ;
959
+
960
+ await Promise . all ( [ first , second , done ] ) ;
961
+ assertEquals ( c . serial , 2 ) ;
962
+ assertEquals ( resets , 1 ) ;
963
+ assertEquals ( sequences , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ) ;
964
+ await cleanup ( ns , nc ) ;
965
+ } ) ;
966
+
967
+ Deno . test ( "ordered consumers - consume reset" , async ( ) => {
968
+ const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
969
+ const jsm = await jetstreamManager ( nc ) ;
970
+
971
+ await jsm . streams . add ( { name : "A" , subjects : [ "a" ] } ) ;
972
+ const js = jetstream ( nc ) ;
973
+ await js . publish ( "a" , JSON . stringify ( 1 ) ) ;
974
+
975
+ let resets = 0 ;
976
+ function countResets ( iter : ConsumerMessages ) : Promise < void > {
977
+ return ( async ( ) => {
978
+ for await ( const s of await iter . status ( ) ) {
979
+ if ( s . type === ConsumerDebugEvents . Reset ) {
980
+ resets ++ ;
981
+ }
982
+ }
983
+ } ) ( ) ;
984
+ }
985
+
986
+ const c = await js . consumers . get ( "A" ) as OrderedPullConsumerImpl ;
987
+
988
+ // after the first message others will get published
989
+ let iter = await c . consume ( { max_messages : 11 , expires : 5000 } ) ;
990
+ countResets ( iter ) . catch ( ) ;
991
+ const sequences = [ ] ;
992
+ for await ( const m of iter ) {
993
+ sequences . push ( m . json ( ) ) ;
994
+ // mess with the internal state to cause a reset
995
+ if ( m . seq === 1 ) {
996
+ c . cursor . deliver_seq = 3 ;
997
+ const buf = [ ] ;
998
+ for ( let i = 2 ; i < 20 ; i ++ ) {
999
+ buf . push ( js . publish ( "a" , JSON . stringify ( i ) ) ) ;
1000
+ }
1001
+ await Promise . all ( buf ) ;
1002
+ }
1003
+ if ( m . seq === 11 ) {
1004
+ break ;
1005
+ }
1006
+ }
1007
+
1008
+ assertEquals ( c . serial , 2 ) ;
1009
+ assertEquals ( resets , 1 ) ;
1010
+ assertEquals ( sequences , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ) ;
1011
+
1012
+ await cleanup ( ns , nc ) ;
1013
+ } ) ;
1014
+
1015
+ Deno . test ( "ordered consumers - next reset" , async ( ) => {
1016
+ const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
1017
+ const jsm = await jetstreamManager ( nc ) ;
1018
+
1019
+ await jsm . streams . add ( { name : "A" , subjects : [ "a" ] } ) ;
1020
+ const js = jetstream ( nc ) ;
1021
+ await js . publish ( "a" , JSON . stringify ( 1 ) ) ;
1022
+ await js . publish ( "a" , JSON . stringify ( 2 ) ) ;
1023
+
1024
+ const c = await js . consumers . get ( "A" ) as OrderedPullConsumerImpl ;
1025
+
1026
+ // get the first
1027
+ let m = await c . next ( { expires : 1000 } ) ;
1028
+ assertExists ( m ) ;
1029
+ assertEquals ( m . json ( ) , 1 ) ;
1030
+
1031
+ // force a reset
1032
+ c . cursor . deliver_seq = 3 ;
1033
+ await js . publish ( "a" , JSON . stringify ( 2 ) ) ;
1034
+
1035
+ m = await c . next ( { expires : 1000 } ) ;
1036
+ assertEquals ( m , null ) ;
1037
+ assertEquals ( c . serial , 1 ) ;
1038
+
1039
+ await cleanup ( ns , nc ) ;
1040
+ } ) ;
1041
+
1042
+ Deno . test ( "ordered consumers - next reset" , async ( ) => {
1043
+ const { ns, nc } = await _setup ( connect , jetstreamServerConf ( ) ) ;
1044
+ const jsm = await jetstreamManager ( nc ) ;
1045
+
1046
+ await jsm . streams . add ( { name : "A" , subjects : [ "a" ] } ) ;
1047
+ const js = jetstream ( nc ) ;
1048
+
1049
+ await js . publish ( "a" , JSON . stringify ( 1 ) ) ;
1050
+ await js . publish ( "a" , JSON . stringify ( 2 ) ) ;
1051
+
1052
+ const c = await js . consumers . get ( "A" ) as OrderedPullConsumerImpl ;
1053
+ await c . next ( ) ;
1054
+ await c . next ( ) ;
1055
+
1056
+ assertEquals ( c . serial , 1 ) ;
1057
+ await c . info ( ) ;
1058
+ assertEquals ( c . serial , 1 ) ;
1059
+
1060
+ await cleanup ( ns , nc ) ;
1061
+ } ) ;
0 commit comments