@@ -247,7 +247,7 @@ var _ = Describe("Commands", func() {
247
247
It ("should Command" , func () {
248
248
cmds , err := client .Command (ctx ).Result ()
249
249
Expect (err ).NotTo (HaveOccurred ())
250
- Expect (len (cmds )).To (BeNumerically ("~" , 200 , 20 ))
250
+ Expect (len (cmds )).To (BeNumerically ("~" , 200 , 25 ))
251
251
252
252
cmd := cmds ["mget" ]
253
253
Expect (cmd .Name ).To (Equal ("mget" ))
@@ -1160,6 +1160,246 @@ var _ = Describe("Commands", func() {
1160
1160
Expect (mSetNX .Val ()).To (Equal (false ))
1161
1161
})
1162
1162
1163
+ It ("should SetWithArgs with TTL" , func () {
1164
+ args := & redis.SetArgs {
1165
+ TTL : 500 * time .Millisecond ,
1166
+ }
1167
+ err := client .SetArgs (ctx , "key" , "hello" , args ).Err ()
1168
+ Expect (err ).NotTo (HaveOccurred ())
1169
+
1170
+ val , err := client .Get (ctx , "key" ).Result ()
1171
+ Expect (err ).NotTo (HaveOccurred ())
1172
+ Expect (val ).To (Equal ("hello" ))
1173
+
1174
+ Eventually (func () error {
1175
+ return client .Get (ctx , "key" ).Err ()
1176
+ }, "2s" , "100ms" ).Should (Equal (redis .Nil ))
1177
+ })
1178
+
1179
+ It ("should SetWithArgs with expiration date" , func () {
1180
+ expireAt := time .Now ().AddDate (1 , 1 , 1 )
1181
+ args := & redis.SetArgs {
1182
+ ExpireAt : expireAt ,
1183
+ }
1184
+ err := client .SetArgs (ctx , "key" , "hello" , args ).Err ()
1185
+ Expect (err ).NotTo (HaveOccurred ())
1186
+
1187
+ val , err := client .Get (ctx , "key" ).Result ()
1188
+ Expect (err ).NotTo (HaveOccurred ())
1189
+ Expect (val ).To (Equal ("hello" ))
1190
+
1191
+ // check the key has an expiration date
1192
+ // (so a TTL value different of -1)
1193
+ ttl := client .TTL (ctx , "key" )
1194
+ Expect (ttl .Err ()).NotTo (HaveOccurred ())
1195
+ Expect (ttl .Val ()).ToNot (Equal (- 1 ))
1196
+ })
1197
+
1198
+ It ("should SetWithArgs with negative expiration date" , func () {
1199
+ args := & redis.SetArgs {
1200
+ ExpireAt : time .Now ().AddDate (- 3 , 1 , 1 ),
1201
+ }
1202
+ // redis accepts a timestamp less than the current date
1203
+ // but returns nil when trying to get the key
1204
+ err := client .SetArgs (ctx , "key" , "hello" , args ).Err ()
1205
+ Expect (err ).NotTo (HaveOccurred ())
1206
+
1207
+ val , err := client .Get (ctx , "key" ).Result ()
1208
+ Expect (err ).To (Equal (redis .Nil ))
1209
+ Expect (val ).To (Equal ("" ))
1210
+ })
1211
+
1212
+ It ("should SetWithArgs with keepttl" , func () {
1213
+ // Set with ttl
1214
+ argsWithTTL := & redis.SetArgs {
1215
+ TTL : 5 * time .Second ,
1216
+ }
1217
+ set := client .SetArgs (ctx , "key" , "hello" , argsWithTTL )
1218
+ Expect (set .Err ()).NotTo (HaveOccurred ())
1219
+ Expect (set .Result ()).To (Equal ("OK" ))
1220
+
1221
+ // Set with keepttl
1222
+ argsWithKeepTTL := & redis.SetArgs {
1223
+ KeepTTL : true ,
1224
+ }
1225
+ set = client .SetArgs (ctx , "key" , "hello" , argsWithKeepTTL )
1226
+ Expect (set .Err ()).NotTo (HaveOccurred ())
1227
+ Expect (set .Result ()).To (Equal ("OK" ))
1228
+
1229
+ ttl := client .TTL (ctx , "key" )
1230
+ Expect (ttl .Err ()).NotTo (HaveOccurred ())
1231
+ // set keepttl will Retain the ttl associated with the key
1232
+ Expect (ttl .Val ().Nanoseconds ()).NotTo (Equal (- 1 ))
1233
+ })
1234
+
1235
+ It ("should SetWithArgs with NX mode and key exists" , func () {
1236
+ err := client .Set (ctx , "key" , "hello" , 0 ).Err ()
1237
+ Expect (err ).NotTo (HaveOccurred ())
1238
+
1239
+ args := & redis.SetArgs {
1240
+ Mode : "nx" ,
1241
+ }
1242
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1243
+ Expect (err ).To (Equal (redis .Nil ))
1244
+ Expect (val ).To (Equal ("" ))
1245
+ })
1246
+
1247
+ It ("should SetWithArgs with NX mode and key does not exist" , func () {
1248
+ args := & redis.SetArgs {
1249
+ Mode : "nx" ,
1250
+ }
1251
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1252
+ Expect (err ).NotTo (HaveOccurred ())
1253
+ Expect (val ).To (Equal ("OK" ))
1254
+ })
1255
+
1256
+ It ("should SetWithArgs with NX mode and GET option" , func () {
1257
+ args := & redis.SetArgs {
1258
+ Mode : "nx" ,
1259
+ Get : true ,
1260
+ }
1261
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1262
+ Expect (err ).To (Equal (proto .RedisError ("ERR syntax error" )))
1263
+ Expect (val ).To (Equal ("" ))
1264
+ })
1265
+
1266
+ It ("should SetWithArgs with expiration, NX mode, and key does not exist" , func () {
1267
+ args := & redis.SetArgs {
1268
+ TTL : 500 * time .Millisecond ,
1269
+ Mode : "nx" ,
1270
+ }
1271
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1272
+ Expect (err ).NotTo (HaveOccurred ())
1273
+ Expect (val ).To (Equal ("OK" ))
1274
+
1275
+ Eventually (func () error {
1276
+ return client .Get (ctx , "key" ).Err ()
1277
+ }, "1s" , "100ms" ).Should (Equal (redis .Nil ))
1278
+ })
1279
+
1280
+ It ("should SetWithArgs with expiration, NX mode, and key exists" , func () {
1281
+ e := client .Set (ctx , "key" , "hello" , 0 )
1282
+ Expect (e .Err ()).NotTo (HaveOccurred ())
1283
+
1284
+ args := & redis.SetArgs {
1285
+ TTL : 500 * time .Millisecond ,
1286
+ Mode : "nx" ,
1287
+ }
1288
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1289
+ Expect (err ).To (Equal (redis .Nil ))
1290
+ Expect (val ).To (Equal ("" ))
1291
+ })
1292
+
1293
+ It ("should SetWithArgs with expiration, NX mode, and GET option" , func () {
1294
+ args := & redis.SetArgs {
1295
+ TTL : 500 * time .Millisecond ,
1296
+ Mode : "nx" ,
1297
+ Get : true ,
1298
+ }
1299
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1300
+ Expect (err ).To (Equal (proto .RedisError ("ERR syntax error" )))
1301
+ Expect (val ).To (Equal ("" ))
1302
+ })
1303
+
1304
+ It ("should SetWithArgs with XX mode and key does not exist" , func () {
1305
+ args := & redis.SetArgs {
1306
+ Mode : "xx" ,
1307
+ }
1308
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1309
+ Expect (err ).To (Equal (redis .Nil ))
1310
+ Expect (val ).To (Equal ("" ))
1311
+ })
1312
+
1313
+ It ("should SetWithArgs with XX mode and key exists" , func () {
1314
+ e := client .Set (ctx , "key" , "hello" , 0 ).Err ()
1315
+ Expect (e ).NotTo (HaveOccurred ())
1316
+
1317
+ args := & redis.SetArgs {
1318
+ Mode : "xx" ,
1319
+ }
1320
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1321
+ Expect (err ).NotTo (HaveOccurred ())
1322
+ Expect (val ).To (Equal ("OK" ))
1323
+ })
1324
+
1325
+ It ("should SetWithArgs with XX mode and GET option, and key exists" , func () {
1326
+ e := client .Set (ctx , "key" , "hello" , 0 ).Err ()
1327
+ Expect (e ).NotTo (HaveOccurred ())
1328
+
1329
+ args := & redis.SetArgs {
1330
+ Mode : "xx" ,
1331
+ Get : true ,
1332
+ }
1333
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1334
+ Expect (err ).NotTo (HaveOccurred ())
1335
+ Expect (val ).To (Equal ("hello" ))
1336
+ })
1337
+
1338
+ It ("should SetWithArgs with XX mode and GET option, and key does not exist" , func () {
1339
+ args := & redis.SetArgs {
1340
+ Mode : "xx" ,
1341
+ Get : true ,
1342
+ }
1343
+
1344
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1345
+ Expect (err ).To (Equal (redis .Nil ))
1346
+ Expect (val ).To (Equal ("" ))
1347
+ })
1348
+
1349
+ It ("should SetWithArgs with expiration, XX mode, GET option, and key does not exist" , func () {
1350
+ args := & redis.SetArgs {
1351
+ TTL : 500 * time .Millisecond ,
1352
+ Mode : "xx" ,
1353
+ Get : true ,
1354
+ }
1355
+
1356
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1357
+ Expect (err ).To (Equal (redis .Nil ))
1358
+ Expect (val ).To (Equal ("" ))
1359
+ })
1360
+
1361
+ It ("should SetWithArgs with expiration, XX mode, GET option, and key exists" , func () {
1362
+ e := client .Set (ctx , "key" , "hello" , 0 )
1363
+ Expect (e .Err ()).NotTo (HaveOccurred ())
1364
+
1365
+ args := & redis.SetArgs {
1366
+ TTL : 500 * time .Millisecond ,
1367
+ Mode : "xx" ,
1368
+ Get : true ,
1369
+ }
1370
+
1371
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1372
+ Expect (err ).NotTo (HaveOccurred ())
1373
+ Expect (val ).To (Equal ("hello" ))
1374
+
1375
+ Eventually (func () error {
1376
+ return client .Get (ctx , "key" ).Err ()
1377
+ }, "1s" , "100ms" ).Should (Equal (redis .Nil ))
1378
+ })
1379
+
1380
+ It ("should SetWithArgs with Get and key does not exist yet" , func () {
1381
+ args := & redis.SetArgs {
1382
+ Get : true ,
1383
+ }
1384
+
1385
+ val , err := client .SetArgs (ctx , "key" , "hello" , args ).Result ()
1386
+ Expect (err ).To (Equal (redis .Nil ))
1387
+ Expect (val ).To (Equal ("" ))
1388
+ })
1389
+
1390
+ It ("should SetWithArgs with Get and key exists" , func () {
1391
+ e := client .Set (ctx , "key" , "hello" , 0 )
1392
+ Expect (e .Err ()).NotTo (HaveOccurred ())
1393
+
1394
+ args := & redis.SetArgs {
1395
+ Get : true ,
1396
+ }
1397
+
1398
+ val , err := client .SetArgs (ctx , "key" , "world" , args ).Result ()
1399
+ Expect (err ).NotTo (HaveOccurred ())
1400
+ Expect (val ).To (Equal ("hello" ))
1401
+ })
1402
+
1163
1403
It ("should Set with expiration" , func () {
1164
1404
err := client .Set (ctx , "key" , "hello" , 100 * time .Millisecond ).Err ()
1165
1405
Expect (err ).NotTo (HaveOccurred ())
@@ -1169,7 +1409,7 @@ var _ = Describe("Commands", func() {
1169
1409
Expect (val ).To (Equal ("hello" ))
1170
1410
1171
1411
Eventually (func () error {
1172
- return client .Get (ctx , "foo " ).Err ()
1412
+ return client .Get (ctx , "key " ).Err ()
1173
1413
}, "1s" , "100ms" ).Should (Equal (redis .Nil ))
1174
1414
})
1175
1415
0 commit comments