FSTMockDatastore.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  19. #include "Firestore/core/src/firebase/firestore/model/types.h"
  20. #include "Firestore/core/src/firebase/firestore/remote/datastore.h"
  21. #include "Firestore/core/src/firebase/firestore/util/status.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. namespace firebase {
  24. namespace firestore {
  25. namespace remote {
  26. class MockWatchStream;
  27. class MockWriteStream;
  28. class MockDatastore : public Datastore {
  29. public:
  30. MockDatastore(const core::DatabaseInfo& database_info,
  31. util::AsyncQueue* worker_queue,
  32. auth::CredentialsProvider* credentials);
  33. std::shared_ptr<WatchStream> CreateWatchStream(id<FSTWatchStreamDelegate> delegate) override;
  34. std::shared_ptr<WriteStream> CreateWriteStream(id<FSTWriteStreamDelegate> delegate) override;
  35. /**
  36. * A count of the total number of requests sent to the watch stream since the beginning of the
  37. * test case.
  38. */
  39. int watch_stream_request_count() const {
  40. return watch_stream_request_count_;
  41. }
  42. /**
  43. * A count of the total number of requests sent to the write stream since the beginning of the
  44. * test case.
  45. */
  46. int write_stream_request_count() const {
  47. return write_stream_request_count_;
  48. }
  49. void IncrementWatchStreamRequests() {
  50. ++watch_stream_request_count_;
  51. }
  52. void IncrementWriteStreamRequests() {
  53. ++write_stream_request_count_;
  54. }
  55. /** Injects a WatchChange as though it had come from the backend. */
  56. void WriteWatchChange(FSTWatchChange* change, const model::SnapshotVersion& snap);
  57. /** Injects a stream failure as though it had come from the backend. */
  58. void FailWatchStream(const util::Status& error);
  59. /** Returns the set of active targets on the watch stream. */
  60. NSDictionary<FSTBoxedTargetID*, FSTQueryData*>* ActiveTargets() const;
  61. /** Helper method to expose watch stream state to verify in tests. */
  62. bool IsWatchStreamOpen() const;
  63. /**
  64. * Returns the next write that was "sent to the backend", failing if there are no queued sent
  65. */
  66. NSArray<FSTMutation*>* NextSentWrite();
  67. /** Returns the number of writes that have been sent to the backend but not waited on yet. */
  68. int WritesSent() const;
  69. /** Injects a write ack as though it had come from the backend in response to a write. */
  70. void AckWrite(const model::SnapshotVersion& version, NSArray<FSTMutationResult*>* results);
  71. /** Injects a stream failure as though it had come from the backend. */
  72. void FailWrite(const util::Status& error);
  73. private:
  74. // These are all passed to the base class; however, making `MockDatastore` store the pointers
  75. // reduces the number of test-only methods in `Datastore`.
  76. const core::DatabaseInfo* database_info_ = nullptr;
  77. util::AsyncQueue* worker_queue_ = nullptr;
  78. auth::CredentialsProvider* credentials_ = nullptr;
  79. std::shared_ptr<MockWatchStream> watch_stream_;
  80. std::shared_ptr<MockWriteStream> write_stream_;
  81. int watch_stream_request_count_ = 0;
  82. int write_stream_request_count_ = 0;
  83. };
  84. } // namespace remote
  85. } // namespace firestore
  86. } // namespace firebase
  87. NS_ASSUME_NONNULL_END