File tree 3 files changed +46
-0
lines changed
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -338,6 +338,20 @@ class Redis extends Commander {
338
338
return new Redis ( { ...this . options , ...override } ) ;
339
339
}
340
340
341
+ /**
342
+ * Mode of the connection.
343
+ *
344
+ * One of `"normal"`, `"subscriber"`, or `"monitor"`. When the connection is
345
+ * not in `"normal"` mode, certain commands are not allowed.
346
+ */
347
+ get mode ( ) : "normal" | "subscriber" | "monitor" {
348
+ return this . options . monitor
349
+ ? "monitor"
350
+ : this . condition . subscriber
351
+ ? "subscriber"
352
+ : "normal" ;
353
+ }
354
+
341
355
/**
342
356
* Listen for all requests received by the server in real time.
343
357
*
Original file line number Diff line number Diff line change @@ -41,6 +41,18 @@ describe("monitor", () => {
41
41
} ) ;
42
42
} ) ;
43
43
44
+ it ( "should report being in 'monitor' mode" , ( done ) => {
45
+ const redis = new Redis ( ) ;
46
+ redis . monitor ( async ( err , monitor ) => {
47
+ await waitForMonitorReady ( monitor ) ;
48
+ expect ( redis . mode ) . to . equal ( "normal" ) ;
49
+ expect ( monitor . mode ) . to . equal ( "monitor" ) ;
50
+ redis . disconnect ( ) ;
51
+ monitor . disconnect ( ) ;
52
+ done ( ) ;
53
+ } ) ;
54
+ } ) ;
55
+
44
56
it ( "should continue monitoring after reconnection" , ( done ) => {
45
57
const redis = new Redis ( ) ;
46
58
redis . monitor ( ( err , monitor ) => {
Original file line number Diff line number Diff line change @@ -29,6 +29,15 @@ describe("pub/sub", function () {
29
29
} ) ;
30
30
} ) ;
31
31
32
+ it ( "should report being in 'subscriber' mode when subscribed" , ( done ) => {
33
+ const redis = new Redis ( ) ;
34
+ redis . subscribe ( "foo" , function ( ) {
35
+ expect ( redis . mode ) . to . equal ( "subscriber" ) ;
36
+ redis . disconnect ( ) ;
37
+ done ( ) ;
38
+ } ) ;
39
+ } ) ;
40
+
32
41
it ( "should exit subscriber mode using unsubscribe" , ( done ) => {
33
42
const redis = new Redis ( ) ;
34
43
redis . subscribe ( "foo" , "bar" , function ( ) {
@@ -52,6 +61,17 @@ describe("pub/sub", function () {
52
61
} ) ;
53
62
} ) ;
54
63
64
+ it ( "should report being in 'normal' mode after unsubscribing" , ( done ) => {
65
+ const redis = new Redis ( ) ;
66
+ redis . subscribe ( "foo" , "bar" , function ( ) {
67
+ redis . unsubscribe ( "foo" , "bar" , function ( err , count ) {
68
+ expect ( redis . mode ) . to . equal ( "normal" ) ;
69
+ redis . disconnect ( ) ;
70
+ done ( ) ;
71
+ } ) ;
72
+ } ) ;
73
+ } ) ;
74
+
55
75
it ( "should receive messages when subscribe a channel" , ( done ) => {
56
76
const redis = new Redis ( ) ;
57
77
const pub = new Redis ( ) ;
You can’t perform that action at this time.
0 commit comments