userver: userver/storages/redis/mock_client_base.hpp Source File
Loading...
Searching...
No Matches
mock_client_base.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <chrono>
9#include <memory>
10#include <string>
11
14
16#include <userver/storages/redis/mock_request.hpp>
17#include <userver/storages/redis/mock_transaction_impl_base.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace storages::redis {
23
41// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
42class MockClientBase : public Client, public std::enable_shared_from_this<MockClientBase> {
43public:
44 MockClientBase();
45
46 ~MockClientBase() override;
47
48 void WaitConnectedOnce(RedisWaitConnected wait_connected) override;
49 bool IsReady(const HealthCheckParams& params) const override;
50
51 size_t ShardsCount() const override;
52 bool IsInClusterMode() const override;
53
54 size_t ShardByKey(const std::string& key) const override;
55
56 // redis commands:
57
58 RequestAppend Append(std::string key, std::string value, const CommandControl& command_control) override;
59
60 RequestBitop Bitop(
61 BitOperation op,
62 std::string dest_key,
63 std::vector<std::string> src_keys,
64 const CommandControl& command_control
65 ) override;
66
67 RequestDbsize Dbsize(size_t shard, const CommandControl& command_control) override;
68
69 RequestDecr Decr(std::string key, const CommandControl& command_control) override;
70
71 RequestDel Del(std::string key, const CommandControl& command_control) override;
72
73 RequestDel Del(std::vector<std::string> keys, const CommandControl& command_control) override;
74
75 RequestUnlink Unlink(std::string key, const CommandControl& command_control) override;
76
77 RequestUnlink Unlink(std::vector<std::string> keys, const CommandControl& command_control) override;
78
79 RequestEvalCommon EvalCommon(
80 std::string script,
81 std::vector<std::string> keys,
82 std::vector<std::string> args,
83 const CommandControl& command_control
84 ) override;
85 RequestEvalShaCommon EvalShaCommon(
86 std::string script,
87 std::vector<std::string> keys,
88 std::vector<std::string> args,
89 const CommandControl& command_control
90 ) override;
91 RequestEvalCommon EvalReadOnlyCommon(
92 std::string script,
93 std::vector<std::string> keys,
94 std::vector<std::string> args,
95 const CommandControl& command_control
96 ) override;
97 RequestEvalShaCommon EvalShaReadOnlyCommon(
98 std::string script,
99 std::vector<std::string> keys,
100 std::vector<std::string> args,
101 const CommandControl& command_control
102 ) override;
103 RequestGenericCommon GenericCommon(
104 std::string command,
105 std::vector<std::string> args,
106 size_t key_index,
107 const CommandControl& command_control
108 ) override;
109
110 RequestScriptLoad ScriptLoad(std::string script, size_t shard, const CommandControl& command_control) override;
111
112 RequestExists Exists(std::string key, const CommandControl& command_control) override;
113
114 RequestExists Exists(std::vector<std::string> keys, const CommandControl& command_control) override;
115
116 RequestExpire Expire(std::string key, std::chrono::seconds ttl, const CommandControl& command_control) override;
117
118 RequestExpire Expire(
119 std::string key,
120 std::chrono::seconds ttl,
121 ExpireOptions options,
122 const CommandControl& command_control
123 ) override;
124
125 RequestGeoadd Geoadd(std::string key, GeoaddArg point_member, const CommandControl& command_control) override;
126
127 RequestGeoadd Geoadd(std::string key, std::vector<GeoaddArg> point_members, const CommandControl& command_control)
128 override;
129
130 RequestGeopos Geopos(std::string key, std::vector<std::string> members, const CommandControl& command_control)
131 override;
132
133 RequestGeoradius Georadius(
134 std::string key,
135 Longitude lon,
136 Latitude lat,
137 double radius,
138 const GeoradiusOptions& georadius_options,
139 const CommandControl& command_control
140 ) override;
141
142 RequestGeosearch Geosearch(
143 std::string key,
144 std::string member,
145 double radius,
146 const GeosearchOptions& geosearch_options,
147 const CommandControl& command_control
148 ) override;
149
150 RequestGeosearch Geosearch(
151 std::string key,
152 std::string member,
153 BoxWidth width,
154 BoxHeight height,
155 const GeosearchOptions& geosearch_options,
156 const CommandControl& command_control
157 ) override;
158
159 RequestGeosearch Geosearch(
160 std::string key,
161 Longitude lon,
162 Latitude lat,
163 double radius,
164 const GeosearchOptions& geosearch_options,
165 const CommandControl& command_control
166 ) override;
167
168 RequestGeosearch Geosearch(
169 std::string key,
170 Longitude lon,
171 Latitude lat,
172 BoxWidth width,
173 BoxHeight height,
174 const GeosearchOptions& geosearch_options,
175 const CommandControl& command_control
176 ) override;
177
178 RequestGet Get(std::string key, const CommandControl& command_control) override;
179
180 RequestGetdel Getdel(std::string key, const CommandControl& command_control) override;
181
182 RequestGetset Getset(std::string key, std::string value, const CommandControl& command_control) override;
183
184 RequestHdel Hdel(std::string key, std::string field, const CommandControl& command_control) override;
185
186 RequestHdel Hdel(std::string key, std::vector<std::string> fields, const CommandControl& command_control) override;
187
188 RequestHexists Hexists(std::string key, std::string field, const CommandControl& command_control) override;
189
190 RequestHget Hget(std::string key, std::string field, const CommandControl& command_control) override;
191
192 RequestHgetall Hgetall(std::string key, const CommandControl& command_control) override;
193
194 RequestHincrby Hincrby(std::string key, std::string field, int64_t increment, const CommandControl& command_control)
195 override;
196
197 RequestHincrbyfloat Hincrbyfloat(
198 std::string key,
199 std::string field,
200 double increment,
201 const CommandControl& command_control
202 ) override;
203
204 RequestHkeys Hkeys(std::string key, const CommandControl& command_control) override;
205
206 RequestHlen Hlen(std::string key, const CommandControl& command_control) override;
207
208 RequestHmget Hmget(std::string key, std::vector<std::string> fields, const CommandControl& command_control)
209 override;
210
211 RequestHmset Hmset(
212 std::string key,
213 std::vector<std::pair<std::string, std::string>> field_values,
214 const CommandControl& command_control
215 ) override;
216
217 ScanRequest<ScanTag::kHscan> Hscan(std::string key, HscanOptions options, const CommandControl& command_control)
218 override;
219
220 RequestHset Hset(std::string key, std::string field, std::string value, const CommandControl& command_control)
221 override;
222
223 RequestHsetnx Hsetnx(std::string key, std::string field, std::string value, const CommandControl& command_control)
224 override;
225
226 RequestHvals Hvals(std::string key, const CommandControl& command_control) override;
227
228 RequestIncr Incr(std::string key, const CommandControl& command_control) override;
229
230 RequestKeys Keys(std::string keys_pattern, size_t shard, const CommandControl& command_control) override;
231
232 RequestLindex Lindex(std::string key, int64_t index, const CommandControl& command_control) override;
233
234 RequestLlen Llen(std::string key, const CommandControl& command_control) override;
235
236 RequestLpop Lpop(std::string key, const CommandControl& command_control) override;
237
238 RequestLpush Lpush(std::string key, std::string value, const CommandControl& command_control) override;
239
240 RequestLpush Lpush(std::string key, std::vector<std::string> values, const CommandControl& command_control)
241 override;
242
243 RequestLpushx Lpushx(std::string key, std::string element, const CommandControl& command_control) override;
244
245 RequestLrange Lrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override;
246
247 RequestLrem Lrem(std::string key, int64_t count, std::string element, const CommandControl& command_control)
248 override;
249
250 RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override;
251
252 RequestMget Mget(std::vector<std::string> keys, const CommandControl& command_control) override;
253
254 RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values, const CommandControl& command_control)
255 override;
256
257 RequestMsetex Msetex(
258 std::vector<std::pair<std::string, std::string>> key_values,
259 const CommandControl& command_control
260 ) override;
261
262 RequestMsetex Msetex(
263 std::vector<std::pair<std::string, std::string>> key_values,
264 MsetexOptions options,
265 const CommandControl& command_control
266 ) override;
267
268 RequestPersist Persist(std::string key, const CommandControl& command_control) override;
269
270 RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl, const CommandControl& command_control)
271 override;
272
273 RequestPing Ping(size_t shard, const CommandControl& command_control) override;
274
275 RequestPingMessage Ping(size_t shard, std::string message, const CommandControl& command_control) override;
276
277 void Publish(std::string channel, std::string message, const CommandControl& command_control, PubShard policy)
278 override;
279
280 void Spublish(std::string channel, std::string message, const CommandControl& command_control) override;
281
282 RequestRename Rename(std::string key, std::string new_key, const CommandControl& command_control) override;
283
284 RequestRpop Rpop(std::string key, const CommandControl& command_control) override;
285
286 RequestRpush Rpush(std::string key, std::string value, const CommandControl& command_control) override;
287
288 RequestRpush Rpush(std::string key, std::vector<std::string> values, const CommandControl& command_control)
289 override;
290
291 RequestRpushx Rpushx(std::string key, std::string element, const CommandControl& command_control) override;
292
293 RequestSadd Sadd(std::string key, std::string member, const CommandControl& command_control) override;
294
295 RequestSadd Sadd(std::string key, std::vector<std::string> members, const CommandControl& command_control) override;
296
297 ScanRequest<ScanTag::kScan> Scan(size_t shard, ScanOptions options, const CommandControl& command_control) override;
298
299 RequestScard Scard(std::string key, const CommandControl& command_control) override;
300
301 RequestSet Set(std::string key, std::string value, const CommandControl& command_control) override;
302
303 RequestSet Set(
304 std::string key,
305 std::string value,
306 std::chrono::milliseconds ttl,
307 const CommandControl& command_control
308 ) override;
309
310 RequestSetIfExist SetIfExist(std::string key, std::string value, const CommandControl& command_control) override;
311
312 RequestSetIfExist SetIfExist(
313 std::string key,
314 std::string value,
315 std::chrono::milliseconds ttl,
316 const CommandControl& command_control
317 ) override;
318
319 RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, const CommandControl& command_control)
320 override;
321
322 RequestSetIfNotExist SetIfNotExist(
323 std::string key,
324 std::string value,
325 std::chrono::milliseconds ttl,
326 const CommandControl& command_control
327 ) override;
328
329 RequestSetIfNotExistOrGet SetIfNotExistOrGet(
330 std::string key,
331 std::string value,
332 const CommandControl& command_control
333 ) override;
334
335 RequestSetIfNotExistOrGet SetIfNotExistOrGet(
336 std::string key,
337 std::string value,
338 std::chrono::milliseconds ttl,
339 const CommandControl& command_control
340 ) override;
341
342 RequestSetex Setex(
343 std::string key,
344 std::chrono::seconds seconds,
345 std::string value,
346 const CommandControl& command_control
347 ) override;
348
349 RequestSetAndGetPrevious SetAndGetPrevious(
350 std::string key,
351 std::string value,
352 std::chrono::milliseconds ttl,
353 const CommandControl& command_control
354 ) override;
355
356 RequestSismember Sismember(std::string key, std::string member, const CommandControl& command_control) override;
357
358 RequestSmembers Smembers(std::string key, const CommandControl& command_control) override;
359
360 RequestSrandmember Srandmember(std::string key, const CommandControl& command_control) override;
361
362 RequestSrandmembers Srandmembers(std::string key, int64_t count, const CommandControl& command_control) override;
363
364 RequestSrem Srem(std::string key, std::string member, const CommandControl& command_control) override;
365
366 RequestSrem Srem(std::string key, std::vector<std::string> members, const CommandControl& command_control) override;
367
368 ScanRequest<ScanTag::kSscan> Sscan(std::string key, SscanOptions options, const CommandControl& command_control)
369 override;
370
371 RequestStrlen Strlen(std::string key, const CommandControl& command_control) override;
372
373 RequestTime Time(size_t shard, const CommandControl& command_control) override;
374
375 RequestTtl Ttl(std::string key, const CommandControl& command_control) override;
376
377 RequestType Type(std::string key, const CommandControl& command_control) override;
378
379 RequestZadd Zadd(std::string key, double score, std::string member, const CommandControl& command_control) override;
380
381 RequestZadd Zadd(
382 std::string key,
383 double score,
384 std::string member,
385 const ZaddOptions& options,
386 const CommandControl& command_control
387 ) override;
388
389 RequestZadd Zadd(
390 std::string key,
391 std::vector<std::pair<double, std::string>> scored_members,
392 const CommandControl& command_control
393 ) override;
394
395 RequestZadd Zadd(
396 std::string key,
397 std::vector<std::pair<double, std::string>> scored_members,
398 const ZaddOptions& options,
399 const CommandControl& command_control
400 ) override;
401
402 RequestZaddIncr ZaddIncr(std::string key, double score, std::string member, const CommandControl& command_control)
403 override;
404
405 RequestZaddIncrExisting ZaddIncrExisting(
406 std::string key,
407 double score,
408 std::string member,
409 const CommandControl& command_control
410 ) override;
411
412 RequestZcard Zcard(std::string key, const CommandControl& command_control) override;
413
414 RequestZcount Zcount(std::string key, double min, double max, const CommandControl& command_control) override;
415
416 RequestZrange Zrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override;
417
418 RequestZrangeWithScores ZrangeWithScores(
419 std::string key,
420 int64_t start,
421 int64_t stop,
422 const CommandControl& command_control
423 ) override;
424
425 RequestZrangebyscore Zrangebyscore(std::string key, double min, double max, const CommandControl& command_control)
426 override;
427
428 RequestZrangebyscore Zrangebyscore(
429 std::string key,
430 std::string min,
431 std::string max,
432 const CommandControl& command_control
433 ) override;
434
435 RequestZrangebyscore Zrangebyscore(
436 std::string key,
437 double min,
438 double max,
439 const RangeOptions& range_options,
440 const CommandControl& command_control
441 ) override;
442
443 RequestZrangebyscore Zrangebyscore(
444 std::string key,
445 std::string min,
446 std::string max,
447 const RangeOptions& range_options,
448 const CommandControl& command_control
449 ) override;
450
451 RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
452 std::string key,
453 double min,
454 double max,
455 const CommandControl& command_control
456 ) override;
457
458 RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
459 std::string key,
460 std::string min,
461 std::string max,
462 const CommandControl& command_control
463 ) override;
464
465 RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
466 std::string key,
467 double min,
468 double max,
469 const RangeOptions& range_options,
470 const CommandControl& command_control
471 ) override;
472
473 RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
474 std::string key,
475 std::string min,
476 std::string max,
477 const RangeOptions& range_options,
478 const CommandControl& command_control
479 ) override;
480
481 RequestZrem Zrem(std::string key, std::string member, const CommandControl& command_control) override;
482
483 RequestZrem Zrem(std::string key, std::vector<std::string> members, const CommandControl& command_control) override;
484
485 RequestZremrangebyrank Zremrangebyrank(
486 std::string key,
487 int64_t start,
488 int64_t stop,
489 const CommandControl& command_control
490 ) override;
491
492 RequestZremrangebyscore Zremrangebyscore(
493 std::string key,
494 double min,
495 double max,
496 const CommandControl& command_control
497 ) override;
498
499 RequestZremrangebyscore Zremrangebyscore(
500 std::string key,
501 std::string min,
502 std::string max,
503 const CommandControl& command_control
504 ) override;
505
506 ScanRequest<ScanTag::kZscan> Zscan(std::string key, ZscanOptions options, const CommandControl& command_control)
507 override;
508
509 RequestZscore Zscore(std::string key, std::string member, const CommandControl& command_control) override;
510
511 RequestHexpire Hexpire(
512 std::string key,
513 std::chrono::seconds ttl,
514 std::vector<std::string> fields,
515 const CommandControl& command_control
516 ) override;
517
518 RequestHexpire Hexpire(
519 std::string key,
520 std::chrono::seconds ttl,
521 ExpireOptions options,
522 std::vector<std::string> fields,
523 const CommandControl& command_control
524 ) override;
525
526 RequestHexpire Hpexpire(
527 std::string key,
528 std::chrono::milliseconds ttl,
529 std::vector<std::string> fields,
530 const CommandControl& command_control
531 ) override;
532
533 RequestHexpire Hpexpire(
534 std::string key,
535 std::chrono::milliseconds ttl,
536 ExpireOptions options,
537 std::vector<std::string> fields,
538 const CommandControl& command_control
539 ) override;
540
541 RequestHexpire Hexpireat(
542 std::string key,
543 std::chrono::system_clock::time_point deadline,
544 std::vector<std::string> fields,
545 const CommandControl& command_control
546 ) override;
547
548 RequestHexpire Hexpireat(
549 std::string key,
550 std::chrono::system_clock::time_point deadline,
551 ExpireOptions options,
552 std::vector<std::string> fields,
553 const CommandControl& command_control
554 ) override;
555
556 RequestHexpire Hpexpireat(
557 std::string key,
558 std::chrono::system_clock::time_point deadline,
559 std::vector<std::string> fields,
560 const CommandControl& command_control
561 ) override;
562
563 RequestHexpire Hpexpireat(
564 std::string key,
565 std::chrono::system_clock::time_point deadline,
566 ExpireOptions options,
567 std::vector<std::string> fields,
568 const CommandControl& command_control
569 ) override;
570
571 RequestHexpiretime Hexpiretime(
572 std::string key,
573 std::vector<std::string> fields,
574 const CommandControl& command_control
575 ) override;
576
577 RequestHpexpiretime Hpexpiretime(
578 std::string key,
579 std::vector<std::string> fields,
580 const CommandControl& command_control
581 ) override;
582
583 RequestHttl Httl(std::string key, std::vector<std::string> fields, const CommandControl& command_control) override;
584
585 RequestHpttl Hpttl(std::string key, std::vector<std::string> fields, const CommandControl& command_control)
586 override;
587
588 RequestHpersist Hpersist(std::string key, std::vector<std::string> fields, const CommandControl& command_control)
589 override;
590
591 RequestHgetex Hgetex(std::string key, std::vector<std::string> fields, const CommandControl& command_control)
592 override;
593
594 RequestHgetex Hgetex(
595 std::string key,
596 HgetexOptions options,
597 std::vector<std::string> fields,
598 const CommandControl& command_control
599 ) override;
600
601 RequestHsetex Hsetex(
602 std::string key,
603 std::vector<HsetexFieldValue> field_values,
604 const CommandControl& command_control
605 ) override;
606
607 RequestHsetex Hsetex(
608 std::string key,
609 HsetexOptions options,
610 std::vector<HsetexFieldValue> field_values,
611 const CommandControl& command_control
612 ) override;
613
614 RequestJsonSet JsonSet(
615 std::string key,
616 std::string path,
618 const CommandControl& command_control
619 ) override;
620
621 RequestJsonSetIfNotExist JsonSetIfNotExist(
622 std::string key,
623 std::string path,
625 const CommandControl& command_control
626 ) override;
627
628 RequestJsonSetIfExist JsonSetIfExist(
629 std::string key,
630 std::string path,
632 const CommandControl& command_control
633 ) override;
634
635 RequestJsonGet JsonGet(std::string key, const CommandControl& command_control) override;
636
637 RequestJsonGet JsonGet(std::string key, std::string path, const CommandControl& command_control) override;
638
639 RequestJsonGet JsonGet(std::string key, std::vector<std::string> paths, const CommandControl& command_control)
640 override;
641
642 RequestJsonMget JsonMget(std::vector<std::string> keys, std::string path, const CommandControl& command_control)
643 override;
644
645 RequestJsonMset JsonMset(std::vector<JsonKeyPathValue> key_path_values, const CommandControl& command_control)
646 override;
647
648 // end of redis commands
649
650 TransactionPtr Multi() final;
651
652 TransactionPtr Multi(Transaction::CheckShards check_shards) final;
653
655 public:
656 virtual ~MockTransactionImplCreatorBase() = default;
657
658 virtual std::unique_ptr<MockTransactionImplBase> operator()() const = 0;
659 };
660
661 template <typename MockTransactionImpl>
662 void SetMockTransactionImplType() {
663 mock_transaction_impl_creator_ = std::make_shared<MockTransactionImplCreator<MockTransactionImpl>>();
664 }
665
666 void SetMockTransactionImplCreator(std::shared_ptr<MockTransactionImplCreatorBase> mock_transaction_impl_creator) {
667 mock_transaction_impl_creator_ = std::move(mock_transaction_impl_creator);
668 }
669
670 MockClientBase(
671 std::shared_ptr<MockTransactionImplCreatorBase> mock_transaction_impl_creator,
672 std::optional<size_t> force_shard_idx
673 );
674
675private:
676 template <typename MockTransactionImpl>
677 class MockTransactionImplCreator : public MockTransactionImplCreatorBase {
678 public:
679 std::unique_ptr<MockTransactionImplBase> operator()() const override {
680 return std::make_unique<MockTransactionImpl>();
681 }
682 };
683
684 std::shared_ptr<MockTransactionImplCreatorBase> mock_transaction_impl_creator_;
685 std::optional<size_t> force_shard_idx_;
686};
687
688} // namespace storages::redis
689
690USERVER_NAMESPACE_END