FSTMockDatastore.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include <memory>
  18. #include <unordered_map>
  19. #include <vector>
  20. #include "Firestore/core/src/model/model_fwd.h"
  21. #include "Firestore/core/src/remote/datastore.h"
  22. #include "Firestore/core/src/util/status_fwd.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. namespace firebase {
  25. namespace firestore {
  26. namespace remote {
  27. class ConnectivityMonitor;
  28. class FirebaseMetadataProvider;
  29. class MockWatchStream;
  30. class MockWriteStream;
  31. class MockDatastore : public Datastore {
  32. public:
  33. MockDatastore(const core::DatabaseInfo& database_info,
  34. const std::shared_ptr<util::AsyncQueue>& worker_queue,
  35. std::shared_ptr<credentials::AuthCredentialsProvider> auth_credentials,
  36. std::shared_ptr<credentials::AppCheckCredentialsProvider> app_check_credentials,
  37. ConnectivityMonitor* connectivity_monitor,
  38. FirebaseMetadataProvider* firebase_metadata_provider);
  39. std::shared_ptr<WatchStream> CreateWatchStream(WatchStreamCallback* callback) override;
  40. std::shared_ptr<WriteStream> CreateWriteStream(WriteStreamCallback* callback) override;
  41. /**
  42. * A count of the total number of requests sent to the watch stream since the beginning of the
  43. * test case.
  44. */
  45. int watch_stream_request_count() const {
  46. return watch_stream_request_count_;
  47. }
  48. /**
  49. * A count of the total number of requests sent to the write stream since the beginning of the
  50. * test case.
  51. */
  52. int write_stream_request_count() const {
  53. return write_stream_request_count_;
  54. }
  55. void IncrementWatchStreamRequests() {
  56. ++watch_stream_request_count_;
  57. }
  58. void IncrementWriteStreamRequests() {
  59. ++write_stream_request_count_;
  60. }
  61. /** Injects a WatchChange as though it had come from the backend. */
  62. void WriteWatchChange(const WatchChange& change, const model::SnapshotVersion& snap);
  63. /** Injects a stream failure as though it had come from the backend. */
  64. void FailWatchStream(const util::Status& error);
  65. /** Returns the set of active targets on the watch stream. */
  66. const std::unordered_map<model::TargetId, local::TargetData>& ActiveTargets() const;
  67. /** Helper method to expose watch stream state to verify in tests. */
  68. bool IsWatchStreamOpen() const;
  69. /**
  70. * Returns the next write that was "sent to the backend", failing if there are no queued sent
  71. */
  72. std::vector<model::Mutation> NextSentWrite();
  73. /** Returns the number of writes that have been sent to the backend but not waited on yet. */
  74. int WritesSent() const;
  75. /** Injects a write ack as though it had come from the backend in response to a write. */
  76. void AckWrite(const model::SnapshotVersion& version, std::vector<model::MutationResult> results);
  77. /** Injects a stream failure as though it had come from the backend. */
  78. void FailWrite(const util::Status& error);
  79. private:
  80. // These are all passed to the base class; however, making `MockDatastore` store the pointers
  81. // reduces the number of test-only methods in `Datastore`.
  82. const core::DatabaseInfo* database_info_ = nullptr;
  83. std::shared_ptr<util::AsyncQueue> worker_queue_;
  84. std::shared_ptr<credentials::AppCheckCredentialsProvider> app_check_credentials_;
  85. std::shared_ptr<credentials::AuthCredentialsProvider> auth_credentials_;
  86. std::shared_ptr<MockWatchStream> watch_stream_;
  87. std::shared_ptr<MockWriteStream> write_stream_;
  88. int watch_stream_request_count_ = 0;
  89. int write_stream_request_count_ = 0;
  90. };
  91. } // namespace remote
  92. } // namespace firestore
  93. } // namespace firebase
  94. NS_ASSUME_NONNULL_END