-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathsync-errors.cpp
258 lines (233 loc) · 10 KB
/
sync-errors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <unistd.h>
#include <catch2/catch_test_macros.hpp>
#include <cpprealm/sdk.hpp>
#include <future>
#include <string>
// :replace-start: {
// "terms": {
// "SyncError_": ""
// }
// }
static const std::string APP_ID = "cpp-tester-uliix";
namespace realm {
struct SyncError_Dog {
realm::primary_key<realm::object_id> _id{realm::object_id::generate()};
std::string name;
int64_t age;
};
REALM_SCHEMA(SyncError_Dog, _id, name, age)
// :snippet-start: compensating-write-model
struct SyncError_Item {
realm::primary_key<realm::object_id> _id{realm::object_id::generate()};
std::string ownerId;
std::string itemName;
int64_t complexity;
};
REALM_SCHEMA(SyncError_Item, _id, ownerId, itemName, complexity)
// :snippet-end:
} // namespace realm
TEST_CASE("set a sync error handler", "[error]") {
// :snippet-start: create-error-handler
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto dbConfig = user.flexible_sync_configuration();
// Setting an error handler on the sync_config gives you access to
// sync_session and sync_error
dbConfig.sync_config().set_error_handler(
[](const realm::sync_session &session,
const realm::internal::bridge::sync_error &error) {
std::cerr << "A sync error occurred. Message: " << error.message()
<< std::endl;
});
auto syncRealm = realm::db(dbConfig);
// :snippet-end:
auto syncSession = syncRealm.get_sync_session();
auto updateSubscriptionSuccess =
syncRealm.subscriptions()
.update(
[](realm::mutable_sync_subscription_set &subs) { subs.clear(); })
.get();
CHECK(updateSubscriptionSuccess == true);
CHECK(syncRealm.subscriptions().size() == 0);
syncSession->wait_for_upload_completion().get();
updateSubscriptionSuccess =
syncRealm.subscriptions()
.update([](realm::mutable_sync_subscription_set &subs) {
subs.add<realm::SyncError_Dog>("dogs");
})
.get();
REQUIRE(updateSubscriptionSuccess == true);
CHECK(syncRealm.subscriptions().size() == 1);
auto dog = realm::SyncError_Dog{.name = "Maui", .age = 4};
// This should trigger a compensating write error when it tries to sync
// due to server-side permissions, which gets logged with the error handler.
syncRealm.write([&] { syncRealm.add(std::move(dog)); });
syncSession->wait_for_upload_completion().get();
}
TEST_CASE("compensating write error outside query", "[error]") {
// :snippet-start: compensating-write-setup
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto dbConfig = user.flexible_sync_configuration();
// :remove-start:
realm::object_id primaryKey = {realm::object_id::generate()};
std::promise<realm::internal::bridge::sync_error> errorPromise;
std::future<realm::internal::bridge::sync_error> future =
errorPromise.get_future();
dbConfig.sync_config().set_error_handler(
[&](const realm::sync_session &session,
std::optional<realm::internal::bridge::sync_error> error) {
std::cerr << "A sync error occurred. Message: " << error->message()
<< std::endl;
errorPromise.set_value(*error); // :remove:
});
// :remove-end:
auto syncRealm = realm::db(dbConfig);
// :remove-start:
auto syncSession = syncRealm.get_sync_session();
auto updateSubscriptionSuccess =
syncRealm.subscriptions()
.update(
[](realm::mutable_sync_subscription_set &subs) { subs.clear(); })
.get();
CHECK(updateSubscriptionSuccess == true);
CHECK(syncRealm.subscriptions().size() == 0);
syncSession->wait_for_upload_completion().get();
// :remove-end:
// Add subscription
auto subscriptionUpdateSuccess =
syncRealm.subscriptions()
.update([](realm::mutable_sync_subscription_set &subs) {
// Get Items from Atlas that match this query.
// Uses the queryable field `complexity`.
// Sync Item objects with complexity less than or equal to 4.
subs.add<realm::SyncError_Item>(
"simple items", [](auto &obj) { return obj.complexity <= 4; });
})
.get();
// :snippet-end:
REQUIRE(subscriptionUpdateSuccess == true);
CHECK(syncRealm.subscriptions().size() == 1);
// :snippet-start: successful-write-example
// Per the Device Sync permissions, users can only read and write data
// where the `Item.ownerId` property matches their own user ID.
auto simpleItem =
realm::SyncError_Item{.ownerId = user.identifier(),
.itemName = "This item meets sync criteria",
.complexity = 3};
// `simpleItem` successfully writes to the realm and syncs to Atlas
// because its data matches the subscription query (complexity <= 4)
// and its `ownerId` field matches the user ID.
syncRealm.write([&] { syncRealm.add(std::move(simpleItem)); });
// :snippet-end:
auto syncedItems = syncRealm.objects<realm::SyncError_Item>();
CHECK(syncedItems.size() == 1);
auto specificItem = syncedItems[0];
syncRealm.write([&] { syncRealm.remove(specificItem); });
syncSession->wait_for_upload_completion().get();
// :snippet-start: compensating-write-example
// The complexity of this item is `7`. This is outside the bounds
// of the subscription query, which triggers a compensating write.
auto complexItem =
realm::SyncError_Item{._id = primaryKey,
.ownerId = user.identifier(),
.itemName = "Test compensating writes",
.complexity = 7};
// This should trigger a compensating write error when it tries to sync
// due to server-side permissions, which gets logged with the error handler.
syncRealm.write([&] { syncRealm.add(std::move(complexItem)); });
// :snippet-end:
syncSession->wait_for_upload_completion().get();
std::string pkString = primaryKey.to_string();
auto receivedSyncError = future.get();
// :snippet-start: get-compensating-write-error-info
auto info = receivedSyncError.compensating_writes_info();
for (auto &v : info) {
std::cout << "A write was rejected with a compensating write error.\n";
std::cout << "An object of type " << v.object_name << "\n";
std::cout << "was rejected because " << v.reason << ".\n";
// :remove-start:
REQUIRE(v.primary_key == realm::object_id(primaryKey));
REQUIRE(v.object_name == "SyncError_Item");
REQUIRE(v.reason == "write to ObjectID(\"" + pkString +
"\") in table \"SyncError_Item\" not allowed; "
"object is outside of the current query view");
// :remove-end:
}
// :snippet-end:
}
TEST_CASE("compensating write error write doesn't match permissions",
"[error]") {
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto dbConfig = user.flexible_sync_configuration();
realm::object_id primaryKey = {realm::object_id::generate()};
std::promise<realm::internal::bridge::sync_error> errorPromise;
std::future<realm::internal::bridge::sync_error> future =
errorPromise.get_future();
dbConfig.sync_config().set_error_handler(
[&](const realm::sync_session &session,
std::optional<realm::internal::bridge::sync_error> error) {
std::cerr << "A sync error occurred. Message: " << error->message()
<< std::endl;
errorPromise.set_value(*error);
});
auto syncRealm = realm::db(dbConfig);
auto syncSession = syncRealm.get_sync_session();
auto updateSubscriptionSuccess =
syncRealm.subscriptions()
.update(
[](realm::mutable_sync_subscription_set &subs) { subs.clear();
})
.get();
CHECK(updateSubscriptionSuccess == true);
CHECK(syncRealm.subscriptions().size() == 0);
syncSession->wait_for_upload_completion().get();
// Add subscription
auto subscriptionUpdateSuccess =
syncRealm.subscriptions()
.update([](realm::mutable_sync_subscription_set &subs) {
// Get Items from Atlas that match this query.
// Uses the queryable field `complexity`.
// Sync Item objects with complexity less than or equal to 4.
subs.add<realm::SyncError_Item>(
"simple items", [](auto &obj) { return obj.complexity <= 4;
});
})
.get();
REQUIRE(subscriptionUpdateSuccess == true);
CHECK(syncRealm.subscriptions().size() == 1);
// :snippet-start: write-not-matching-permissions
// The `ownerId` of this item does not match the user ID of the logged-in
// user. The user does not have permissions to make this write, which
// triggers a compensating write.
auto itemWithWrongOwner = realm::SyncError_Item{
._id = primaryKey, // :remove:
.ownerId = "not the current user",
.itemName = "Trigger an incorrect permissions compensating write",
.complexity = 1};
syncRealm.write([&] { syncRealm.add(std::move(itemWithWrongOwner)); });
// :snippet-end:
syncSession->wait_for_upload_completion().get();
std::string pkString = primaryKey.to_string();
auto receivedSyncError = future.get();
auto info = receivedSyncError.compensating_writes_info();
for (auto &v : info) {
std::cout << "A write was rejected with a compensating write error.\n";
std::cout << "An object of type " << v.object_name << "\n";
std::cout << "was rejected because " << v.reason << ".\n";
// :remove-start:
REQUIRE(v.primary_key == realm::object_id(primaryKey));
REQUIRE(v.object_name == "SyncError_Item");
REQUIRE(v.reason == "write to ObjectID(\"" + pkString +
"\") in table \"SyncError_Item\" not allowed");
// :remove-end:
}
}
// :replace-end: