Skip to content

Commit 3a72090

Browse files
authored
gRPC: add more unit tests to gRPC wrapper classes (#1924)
Add pretty exhaustive unit tests to `GrpcStream`, `GrpcStreamingReader`, and `GrpcUnaryCall`. Also: * when using `ForceFinish` to quickly do completions, verify that the completion that got off the queue has the expected type (this was previously just a comment). The difficulty with doing this in a more deterministic manner is that gRPC doesn't guarantee the order in which tags come off the completion queue. This mostly affects cases when there's a read and a write operation in progress at the same time. To work around this, `ForceFinish` now can also take a callback. * use the ability to force-set a status on a completion to verify that the observer/callback receives the expected status; * add the ability to force-set a received message on a completion to verify that the observer/callback receives the expected message; * add comments about the usage of `ForceFinish`; * Add `CreateNoOpConnectivityMonitor` helper function to `util` in tests.
1 parent 2dc0cac commit 3a72090

File tree

12 files changed

+1029
-231
lines changed

12 files changed

+1029
-231
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@
201201
B6152AD7202A53CB000E5744 /* document_key_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6152AD5202A5385000E5744 /* document_key_test.cc */; };
202202
B65D34A9203C995B0076A5E1 /* FIRTimestampTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B65D34A7203C99090076A5E1 /* FIRTimestampTest.m */; };
203203
B66D8996213609EE0086DA0C /* stream_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = B66D8995213609EE0086DA0C /* stream_test.mm */; };
204+
B67BF449216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */; };
205+
B67BF44A216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */; };
204206
B686F2AF2023DDEE0028D6BE /* field_path_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B686F2AD2023DDB20028D6BE /* field_path_test.cc */; };
205207
B686F2B22025000D0028D6BE /* resource_path_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B686F2B02024FFD70028D6BE /* resource_path_test.cc */; };
206208
B6BBE43121262CF400C6A53E /* grpc_stream_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6BBE42F21262CF400C6A53E /* grpc_stream_test.cc */; };
@@ -516,6 +518,8 @@
516518
B6152AD5202A5385000E5744 /* document_key_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = document_key_test.cc; sourceTree = "<group>"; };
517519
B65D34A7203C99090076A5E1 /* FIRTimestampTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRTimestampTest.m; sourceTree = "<group>"; };
518520
B66D8995213609EE0086DA0C /* stream_test.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = stream_test.mm; sourceTree = "<group>"; };
521+
B67BF447216EB42F00CA9097 /* create_noop_connectivity_monitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = create_noop_connectivity_monitor.h; sourceTree = "<group>"; };
522+
B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = create_noop_connectivity_monitor.cc; sourceTree = "<group>"; };
519523
B686F2AD2023DDB20028D6BE /* field_path_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = field_path_test.cc; sourceTree = "<group>"; };
520524
B686F2B02024FFD70028D6BE /* resource_path_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = resource_path_test.cc; sourceTree = "<group>"; };
521525
B6BBE42F21262CF400C6A53E /* grpc_stream_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = grpc_stream_test.cc; sourceTree = "<group>"; };
@@ -672,6 +676,8 @@
672676
54740A561FC913EB00713A1A /* util */ = {
673677
isa = PBXGroup;
674678
children = (
679+
B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */,
680+
B67BF447216EB42F00CA9097 /* create_noop_connectivity_monitor.h */,
675681
B6FB4680208EA0BE00554BA2 /* async_queue_libdispatch_test.mm */,
676682
B6FB4681208EA0BE00554BA2 /* async_queue_std_test.cc */,
677683
B6FB467B208E9A8200554BA2 /* async_queue_test.cc */,
@@ -1954,6 +1960,7 @@
19541960
549CCA5120A36DBC00BCEB75 /* tree_sorted_map_test.cc in Sources */,
19551961
B6D9649121544D4F00EB9CFB /* grpc_connection_test.cc in Sources */,
19561962
C80B10E79CDD7EF7843C321E /* type_traits_apple_test.mm in Sources */,
1963+
B67BF449216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */,
19571964
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */,
19581965
618BBEAD20B89AAC00B5BCE7 /* write.pb.cc in Sources */,
19591966
);
@@ -1992,6 +1999,7 @@
19921999
5492E081202154EC00B64F25 /* FSTStreamTests.mm in Sources */,
19932000
5492E07F202154EC00B64F25 /* FSTTransactionTests.mm in Sources */,
19942001
5492E0442021457E00B64F25 /* XCTestCase+Await.mm in Sources */,
2002+
B67BF44A216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */,
19952003
EBFC611B1BF195D0EC710AF4 /* app_testing.mm in Sources */,
19962004
CA989C0E6020C372A62B7062 /* testutil.cc in Sources */,
19972005
);

Firestore/core/src/firebase/firestore/remote/connectivity_monitor.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Firestore/core/src/firebase/firestore/remote/connectivity_monitor.h"
1818

1919
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
20+
#include "absl/memory/memory.h"
2021

2122
namespace firebase {
2223
namespace firestore {

Firestore/core/src/firebase/firestore/remote/grpc_stream.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ bool GrpcStream::WriteAndFinish(grpc::ByteBuffer&& message) {
227227
// Only bother with the last write if there is no active write at the moment.
228228
if (maybe_write) {
229229
BufferedWrite last_write = std::move(maybe_write).value();
230-
auto* completion = new GrpcCompletion(Type::Write, worker_queue_, {});
230+
GrpcCompletion* completion = NewCompletion(Type::Write, {});
231231
*completion->message() = last_write.message;
232232
call_->WriteLast(*completion->message(), grpc::WriteOptions{}, completion);
233233

Firestore/core/test/firebase/firestore/remote/grpc_connection_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ TEST_F(GrpcConnectionTest, GrpcStreamingCallsNoticeChangeInConnectivity) {
138138
EXPECT_EQ(change_count, 1);
139139
}
140140

141+
TEST_F(GrpcConnectionTest, GrpcUnaryCallsNoticeChangeInConnectivity) {
142+
int change_count = 0;
143+
std::unique_ptr<GrpcUnaryCall> unary_call = tester.CreateUnaryCall();
144+
unary_call->Start([&](const StatusOr<grpc::ByteBuffer>& result) {
145+
if (IsConnectivityChange(result.status())) {
146+
++change_count;
147+
}
148+
});
149+
150+
SetNetworkStatus(NetworkStatus::Available);
151+
// Same status shouldn't trigger a callback.
152+
EXPECT_EQ(change_count, 0);
153+
154+
tester.KeepPollingGrpcQueue();
155+
SetNetworkStatus(NetworkStatus::AvailableViaCellular);
156+
EXPECT_EQ(change_count, 1);
157+
}
158+
141159
TEST_F(GrpcConnectionTest, ConnectivityChangeWithSeveralActiveCalls) {
142160
int changes_count = 0;
143161

0 commit comments

Comments
 (0)