@@ -98,6 +98,86 @@ async fn key_value_timeout() {
98
98
assert_eq ! ( b"$-1\r \n " , & response) ;
99
99
}
100
100
101
+ // In this case we test that server responds acurately to
102
+ // SUBSCRIBE and UNSUBSCRIBE commands
103
+ #[ tokio:: test]
104
+ async fn subscribe_unsubscribe ( ) {
105
+ let ( addr, _handle) = start_server ( ) . await ;
106
+
107
+ let mut stream = TcpStream :: connect ( addr) . await . unwrap ( ) ;
108
+
109
+ // send SUBSCRIBE command
110
+ stream. write_all ( b"*2\r \n $9\r \n subscribe\r \n $5\r \n hello\r \n " ) . await . unwrap ( ) ;
111
+
112
+ // Read response
113
+ let mut response = [ 0 ; 30 ] ;
114
+
115
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
116
+
117
+ assert_eq ! ( b"*2\r \n $9\r \n subscribe\r \n $5\r \n hello\r \n " , & response) ;
118
+
119
+ // send UNSUBSCRIBE command
120
+ stream. write_all ( b"*2\r \n $11\r \n unsubscribe\r \n $5\r \n hello\r \n " ) . await . unwrap ( ) ;
121
+
122
+ let mut response = [ 0 ; 33 ] ;
123
+
124
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
125
+
126
+ assert_eq ! ( b"*2\r \n $11\r \n unsubscribe\r \n " , & response[ 0 ..22 ] ) ;
127
+ assert_eq ! ( b"$5\r \n hello\r \n " , & response[ 22 ..33 ] ) ;
128
+ }
129
+
130
+ // In this case we test that server Responds with an Error message if a client
131
+ // sends an unknown command
132
+ #[ tokio:: test]
133
+ async fn send_error_unknown_command ( ) {
134
+ let ( addr, _handle) = start_server ( ) . await ;
135
+
136
+ // Establish a connection to the server
137
+ let mut stream = TcpStream :: connect ( addr) . await . unwrap ( ) ;
138
+
139
+ // Get a key, data is missing
140
+ stream. write_all ( b"*2\r \n $3\r \n FOO\r \n $5\r \n hello\r \n " ) . await . unwrap ( ) ;
141
+
142
+ let mut response = [ 0 ; 28 ] ;
143
+
144
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
145
+
146
+ assert_eq ! ( b"-ERR unknown command \' foo\' \r \n " , & response) ;
147
+ }
148
+
149
+ // In this case we test that server Responds with an Error message if a client
150
+ // sends an GET or SET command after a SUBSCRIBE
151
+ #[ tokio:: test]
152
+ async fn send_error_get_set_after_subscribe ( ) {
153
+ let ( addr, _handle) = start_server ( ) . await ;
154
+
155
+ let mut stream = TcpStream :: connect ( addr) . await . unwrap ( ) ;
156
+
157
+ // send SUBSCRIBE command
158
+ stream. write_all ( b"*2\r \n $9\r \n subscribe\r \n $5\r \n hello\r \n " ) . await . unwrap ( ) ;
159
+
160
+ let mut response = [ 0 ; 30 ] ;
161
+
162
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
163
+
164
+ assert_eq ! ( b"*2\r \n $9\r \n subscribe\r \n $5\r \n hello\r \n " , & response) ;
165
+
166
+ stream. write_all ( b"*3\r \n $3\r \n SET\r \n $5\r \n hello\r \n $5\r \n world\r \n " ) . await . unwrap ( ) ;
167
+
168
+ let mut response = [ 0 ; 28 ] ;
169
+
170
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
171
+ assert_eq ! ( b"-ERR unknown command \' set\' \r \n " , & response) ;
172
+
173
+ stream. write_all ( b"*2\r \n $3\r \n GET\r \n $5\r \n hello\r \n " ) . await . unwrap ( ) ;
174
+
175
+ let mut response = [ 0 ; 28 ] ;
176
+
177
+ stream. read_exact ( & mut response) . await . unwrap ( ) ;
178
+ assert_eq ! ( b"-ERR unknown command \' get\' \r \n " , & response) ;
179
+ }
180
+
101
181
async fn start_server ( ) -> ( SocketAddr , JoinHandle < mini_redis:: Result < ( ) > > ) {
102
182
let listener = TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
103
183
let addr = listener. local_addr ( ) . unwrap ( ) ;
0 commit comments