• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / jonathan.reams_2947

01 Dec 2023 08:08PM UTC coverage: 91.739% (+0.04%) from 91.695%
jonathan.reams_2947

Pull #7160

Evergreen

jbreams
allow handle_error to decide resumability
Pull Request #7160: Prevent resuming a session that has not been fully shut down

92428 of 169414 branches covered (0.0%)

315 of 349 new or added lines in 14 files covered. (90.26%)

80 existing lines in 14 files now uncovered.

232137 of 253041 relevant lines covered (91.74%)

6882826.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.3
/test/test_lang_bind_helper.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <map>
20
#include <sstream>
21
#include <mutex>
22
#include <condition_variable>
23
#include <atomic>
24
#include <chrono>
25
#include <thread>
26
#include "testsettings.hpp"
27
#ifdef TEST_LANG_BIND_HELPER
28

29
#include <realm.hpp>
30
#include <realm/util/encrypted_file_mapping.hpp>
31
#include <realm/util/to_string.hpp>
32
#include <realm/replication.hpp>
33
#include <realm/util/backtrace.hpp>
34

35
#include "test.hpp"
36
#include "test_table_helper.hpp"
37
#include "util/misc.hpp"
38
#include "util/spawned_process.hpp"
39

40
using namespace realm;
41
using namespace realm::util;
42
using namespace realm::test_util;
43
using unit_test::TestContext;
44

45
// Test independence and thread-safety
46
// -----------------------------------
47
//
48
// All tests must be thread safe and independent of each other. This
49
// is required because it allows for both shuffling of the execution
50
// order and for parallelized testing.
51
//
52
// In particular, avoid using std::rand() since it is not guaranteed
53
// to be thread safe. Instead use the API offered in
54
// `test/util/random.hpp`.
55
//
56
// All files created in tests must use the TEST_PATH macro (or one of
57
// its friends) to obtain a suitable file system path. See
58
// `test/util/test_path.hpp`.
59
//
60
//
61
// Debugging and the ONLY() macro
62
// ------------------------------
63
//
64
// A simple way of disabling all tests except one called `Foo`, is to
65
// replace TEST(Foo) with ONLY(Foo) and then recompile and rerun the
66
// test suite. Note that you can also use filtering by setting the
67
// environment varible `UNITTEST_FILTER`. See `README.md` for more on
68
// this.
69
//
70
// Another way to debug a particular test, is to copy that test into
71
// `experiments/testcase.cpp` and then run `sh build.sh
72
// check-testcase` (or one of its friends) from the command line.
73

74
namespace {
75

76
void work_on_frozen(TestContext& test_context, TransactionRef frozen)
77
{
195✔
78
    CHECK(frozen->is_frozen());
195✔
79
    CHECK_THROW(frozen->promote_to_write(), LogicError);
195✔
80
    auto table = frozen->get_table("my_table");
195✔
81
    CHECK(table->is_frozen());
195✔
82
    auto col = table->get_column_key("my_col_1");
195✔
83
    int64_t sum = 0;
195✔
84
    for (auto i : *table) {
136,496✔
85
        sum += i.get<int64_t>(col);
136,496✔
86
    }
136,496✔
87
    CHECK_EQUAL(sum, 1000 / 2 * 999);
195✔
88
    TableView tv = table->where().not_equal(col, 42).find_all();
195✔
89
    CHECK(tv.is_frozen());
195✔
90
}
195✔
91

92
TEST(Transactions_Frozen)
93
{
2✔
94
    SHARED_GROUP_TEST_PATH(path);
2✔
95
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
96
    DBRef db = DB::create(*hist_w, path);
2✔
97
    TransactionRef frozen;
2✔
98
    {
2✔
99
        auto wt = db->start_write();
2✔
100
        auto table = wt->add_table("my_table");
2✔
101
        auto col = table->add_column(type_Int, "my_col_1");
2✔
102
        for (int j = 0; j < 1000; ++j) {
2,002✔
103
            table->create_object().set_all(j);
2,000✔
104
        }
2,000✔
105
        wt->commit_and_continue_as_read();
2✔
106
        frozen = wt->freeze();
2✔
107
        auto imported_table = frozen->import_copy_of(table);
2✔
108
        CHECK(imported_table->is_frozen());
2✔
109
        TableView tv = table->where().not_equal(col, 42).find_all();
2✔
110
        CHECK(!tv.is_frozen());
2✔
111
        auto imported = frozen->import_copy_of(tv, PayloadPolicy::Move);
2✔
112
        CHECK(frozen->is_frozen());
2✔
113
        CHECK(imported->is_frozen());
2✔
114
        auto imported2 = frozen->import_copy_of(tv, PayloadPolicy::Stay);
2✔
115
        CHECK(!imported2->is_frozen());
2✔
116
        imported2->sync_if_needed();
2✔
117
        CHECK(imported2->is_frozen());
2✔
118
    }
2✔
119
    // create multiple threads, all doing read-only work on Frozen
1✔
120
    const int num_threads = 100;
2✔
121
    std::thread frozen_workers[num_threads];
2✔
122
    for (int j = 0; j < num_threads; ++j)
202✔
123
        frozen_workers[j] = std::thread([&] {
200✔
124
            work_on_frozen(test_context, frozen);
190✔
125
        });
190✔
126
    for (int j = 0; j < num_threads; ++j)
202✔
127
        frozen_workers[j].join();
200✔
128
}
2✔
129

130

131
TEST(Transactions_ConcurrentFrozenTableGetByName)
132
{
2✔
133
    SHARED_GROUP_TEST_PATH(path);
2✔
134
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
135
    DBRef db = DB::create(*hist_w, path);
2✔
136
    TransactionRef frozen;
2✔
137
    std::string table_names[3000];
2✔
138
    {
2✔
139
        auto wt = db->start_write();
2✔
140
        for (int j = 0; j < 3000; ++j) {
6,002✔
141
            std::string name = "Table" + to_string(j);
6,000✔
142
            table_names[j] = name;
6,000✔
143
            wt->add_table(name);
6,000✔
144
        }
6,000✔
145
        wt->commit_and_continue_as_read();
2✔
146
        frozen = wt->freeze();
2✔
147
    }
2✔
148
    auto runner = [&](int first, int last) {
1,973✔
149
        millisleep(1);
1,973✔
150
        for (int j = first; j < last; ++j) {
1,900,938✔
151
            frozen->get_table(table_names[j]);
1,898,965✔
152
        }
1,898,965✔
153
    };
1,973✔
154
    std::thread threads[1000];
2✔
155
    for (int j = 0; j < 1000; ++j) {
2,002✔
156
        threads[j] = std::thread(runner, j * 2, j * 2 + 1000);
2,000✔
157
    }
2,000✔
158
    for (int j = 0; j < 1000; ++j)
2,002✔
159
        threads[j].join();
2,000✔
160
}
2✔
161

162
TEST(Transactions_ReclaimFrozen)
163
{
2✔
164
    struct Entry {
2✔
165
        TransactionRef frozen;
2✔
166
        Obj o;
2✔
167
        int64_t value;
2✔
168
    };
2✔
169
    int num_pending_transactions = 100;
2✔
170
    int num_transactions_created = 1000;
2✔
171
    int num_objects = 200;
2✔
172
    int num_checks_pr_trans = 10;
2✔
173

1✔
174
    SHARED_GROUP_TEST_PATH(path);
2✔
175
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
176
    DBRef db = DB::create(*hist_w, path);
2✔
177
    std::vector<Entry> refs;
2✔
178
    refs.resize(num_pending_transactions);
2✔
179
    Random random(random_int<unsigned long>());
2✔
180

1✔
181
    auto wt = db->start_write();
2✔
182
    auto tbl = wt->add_table("TestTable");
2✔
183
    auto col = tbl->add_column(type_Int, "IntCol");
2✔
184
    Obj o;
2✔
185
    for (int j = 0; j < num_objects; ++j) {
402✔
186
        o = tbl->create_object(ObjKey(j));
400✔
187
        o.set<Int>(col, 10000000000 + j);
400✔
188
    }
400✔
189
    wt->commit_and_continue_as_read();
2✔
190
    for (int j = 0; j < num_transactions_created; ++j) {
2,002✔
191
        int trans_number = random.draw_int_mod(num_pending_transactions);
2,000✔
192
        auto frozen = wt->freeze();
2,000✔
193
        // auto frozen = wt->duplicate();
1,000✔
194
        refs[trans_number].frozen = frozen;
2,000✔
195
        refs[trans_number].o = frozen->import_copy_of(o);
2,000✔
196
        refs[trans_number].value = o.get<Int>(col);
2,000✔
197
        wt->promote_to_write();
2,000✔
198
        int key = random.draw_int_mod(num_objects);
2,000✔
199
        o = tbl->get_object(ObjKey(key));
2,000✔
200
        o.set<Int>(col, o.get<Int>(col) + 42);
2,000✔
201
        wt->commit_and_continue_as_read();
2,000✔
202
        for (int k = 0; k < num_checks_pr_trans; ++k) {
22,000✔
203
            int selected_trans = random.draw_int_mod(num_pending_transactions);
20,000✔
204
            if (refs[selected_trans].frozen) {
20,000✔
205
                CHECK(refs[selected_trans].value == refs[selected_trans].o.get<Int>(col));
17,884✔
206
            }
17,884✔
207
        }
20,000✔
208
    }
2,000✔
209
    for (auto& e : refs) {
200✔
210
        e.frozen.reset();
200✔
211
    }
200✔
212
    wt->promote_to_write();
2✔
213
    wt->commit();
2✔
214
    // frozen = wt->freeze();
1✔
215
}
2✔
216

217
TEST(Transactions_ConcurrentFrozenTableGetByKey)
218
{
2✔
219
    SHARED_GROUP_TEST_PATH(path);
2✔
220
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
221
    DBRef db = DB::create(*hist_w, path);
2✔
222
    TransactionRef frozen;
2✔
223
    TableKey table_keys[3000];
2✔
224
    {
2✔
225
        auto wt = db->start_write();
2✔
226
        for (int j = 0; j < 3000; ++j) {
6,002✔
227
            std::string name = "Table" + to_string(j);
6,000✔
228
            auto table = wt->add_table(name);
6,000✔
229
            table_keys[j] = table->get_key();
6,000✔
230
        }
6,000✔
231
        wt->commit_and_continue_as_read();
2✔
232
        frozen = wt->freeze();
2✔
233
    }
2✔
234
    auto runner = [&](int first, int last) {
1,945✔
235
        millisleep(1);
1,945✔
236
        for (int j = first; j < last; ++j) {
1,569,165✔
237
            auto table = frozen->get_table(table_keys[j]);
1,567,220✔
238
            CHECK(table->get_key() == table_keys[j]);
1,567,220✔
239
        }
1,567,220✔
240
    };
1,945✔
241
    std::thread threads[1000];
2✔
242
    for (int j = 0; j < 1000; ++j) {
2,002✔
243
        threads[j] = std::thread(runner, j * 2, j * 2 + 1000);
2,000✔
244
    }
2,000✔
245
    for (int j = 0; j < 1000; ++j)
2,002✔
246
        threads[j].join();
2,000✔
247
}
2✔
248

249

250
TEST(Transactions_ConcurrentFrozenQueryAndObj)
251
{
2✔
252
    SHARED_GROUP_TEST_PATH(path);
2✔
253
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
254
    DBRef db = DB::create(*hist_w, path);
2✔
255
    TransactionRef frozen;
2✔
256
    ObjKey obj_keys[1000];
2✔
257
    {
2✔
258
        auto wt = db->start_write();
2✔
259
        auto table = wt->add_table("MyTable");
2✔
260
        table->add_column(type_Int, "MyCol");
2✔
261
        for (int i = 0; i < 1000; ++i) {
2,002✔
262
            obj_keys[i] = table->create_object().set_all(i).get_key();
2,000✔
263
        }
2,000✔
264
        wt->commit_and_continue_as_read();
2✔
265
        frozen = wt->freeze();
2✔
266
    }
2✔
267
    auto runner = [&](int first, int last) {
993✔
268
        millisleep(1);
993✔
269
        auto table = frozen->get_table("MyTable");
993✔
270
        auto col = table->get_column_key("MyCol");
993✔
271
        for (int j = first; j < last; ++j) {
444,071✔
272
            // loads of concurrent queries created and executed:
203,523✔
273
            TableView tb = table->where().equal(col, j).find_all();
443,078✔
274
            CHECK(tb.size() == 1);
443,078✔
275
            CHECK(tb.get_key(0) == obj_keys[j]);
443,078✔
276
            // concurrent reads from results are just fine:
203,523✔
277
            auto obj = tb[0];
443,078✔
278
            CHECK(obj.get<Int>(col) == j);
443,078✔
279
        }
443,078✔
280
    };
993✔
281
    std::thread threads[500];
2✔
282
    for (int j = 0; j < 500; ++j) {
1,002✔
283
        threads[j] = std::thread(runner, j, j + 500);
1,000✔
284
    }
1,000✔
285
    for (int j = 0; j < 500; ++j)
1,002✔
286
        threads[j].join();
1,000✔
287
}
2✔
288

289
// this tests resilience against some violations of the Core API.
290
// It creates a lot of races between accessor use and transaction close.
291
// This is undefined behaviour
292
// but the goal is none the less to "harden" Core against just crashing
293
// **           THIS TEST MAY CRASH OCCASIONALLY          **
294
// ** if so, disable it and run it in a different setting **
295
#if 0 // it actually fails occationally
296
TEST_IF(Transactions_ConcurrentFrozenQueryAndObjAndTransactionClose, !REALM_TSAN)
297
{
298
    SHARED_GROUP_TEST_PATH(path);
299
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
300
    DBRef db = DB::create(*hist_w, path);
301
    TransactionRef frozen;
302
    ObjKey obj_keys[1000];
303
    {
304
        auto wt = db->start_write();
305
        auto table = wt->add_table("MyTable");
306
        table->add_column(type_Int, "MyCol");
307
        for (int i = 0; i < 1000; ++i) {
308
            obj_keys[i] = table->create_object().set_all(i).get_key();
309
        }
310
        wt->commit_and_continue_as_read();
311
        frozen = wt->freeze();
312
    }
313
    auto runner = [&](int first, int last) {
314
        millisleep(1);
315
        try {
316
            auto table = frozen->get_table("MyTable");
317
            auto col = table->get_column_key("MyCol");
318
            while (1) {
319
                for (int j = first; j < last; ++j) {
320
                    // loads of concurrent queries created and executed:
321
                    TableView tb = table->where().equal(col, j).find_all();
322
                    CHECK(tb.size() == 1);
323
                    CHECK(tb.get_key(0) == obj_keys[j]);
324
                    // concurrent reads from results are just fine:
325
                    auto obj = tb[0];
326
                    CHECK(obj.get<Int>(col) == j);
327
                }
328
            }
329
        }
330
        catch (NoSuchTable&) {
331
        }
332
        catch (LogicError&) {
333
        }
334
    };
335
    std::thread threads[100];
336
    for (int j = 0; j < 100; ++j) {
337
        threads[j] = std::thread(runner, j, j + 100);
338
    }
339
    millisleep(10);
340
    frozen->close(); // this should cause all threads to throw
341
    for (int j = 0; j < 100; ++j)
342
        threads[j].join();
343
}
344
#endif
345

346
class MyHistory : public _impl::History {
347
public:
348
    MyHistory(const MyHistory&) = delete;
349
    explicit MyHistory(MyHistory* write_history = nullptr)
350
        : m_write_history(write_history)
351
    {
104✔
352
    }
104✔
353
    std::vector<char> m_incoming_changeset;
354
    version_type m_incoming_version;
355
    struct ChangeSet {
356
        std::vector<char> changes;
357
        bool finalized = false;
358
    };
359
    std::map<uint_fast64_t, ChangeSet> m_changesets;
360
    MyHistory* m_write_history = nullptr;
361

362
    void update_from_ref_and_version(ref_type, version_type version) override
363
    {
110✔
364
        update_from_parent(version);
110✔
365
    }
110✔
366
    void update_from_parent(version_type) override
367
    {
110✔
368
        if (m_write_history)
110✔
369
            m_changesets = m_write_history->m_changesets;
108✔
370
    }
110✔
371
    version_type add_changeset(const char* data, size_t size, version_type orig_version)
372
    {
5,008✔
373
        m_incoming_changeset.assign(data, data + size); // Throws
5,008✔
374
        version_type new_version = orig_version + 1;
5,008✔
375
        m_incoming_version = new_version;
5,008✔
376
        // Allocate space for the new changeset in m_changesets such that we can
2,501✔
377
        // be sure no exception will be thrown whan adding the changeset in
2,501✔
378
        // finalize_changeset().
2,501✔
379
        m_changesets[new_version]; // Throws
5,008✔
380
        return new_version;
5,008✔
381
    }
5,008✔
382
    void finalize()
383
    {
5,008✔
384
        // The following operation will not throw due to the space reservation
2,501✔
385
        // carried out in prepare_new_changeset().
2,501✔
386
        m_changesets[m_incoming_version].changes = std::move(m_incoming_changeset);
5,008✔
387
        m_changesets[m_incoming_version].finalized = true;
5,008✔
388
    }
5,008✔
389
    void get_changesets(version_type begin_version, version_type end_version,
390
                        BinaryIterator* buffer) const noexcept override
391
    {
60✔
392
        size_t n = size_t(end_version - begin_version);
60✔
393
        for (size_t i = 0; i < n; ++i) {
120✔
394
            uint_fast64_t version = begin_version + i + 1;
60✔
395
            auto j = m_changesets.find(version);
60✔
396
            REALM_ASSERT(j != m_changesets.end());
60✔
397
            const ChangeSet& changeset = j->second;
60✔
398
            REALM_ASSERT(changeset.finalized); // Must have been finalized
60✔
399
            buffer[i] = BinaryData(changeset.changes.data(), changeset.changes.size());
60✔
400
        }
60✔
401
    }
60✔
402
    void set_oldest_bound_version(version_type) override
403
    {
5,008✔
404
        // No-op
2,501✔
405
    }
5,008✔
406

407
    void verify() const override
408
    {
50✔
409
        // No-op
25✔
410
    }
50✔
411
};
412

413
class ShortCircuitHistory : public Replication {
414
public:
415
    using version_type = _impl::History::version_type;
416

417
    version_type prepare_changeset(const char* data, size_t size, version_type orig_version) override
418
    {
5,008✔
419
        return m_history.add_changeset(data, size, orig_version); // Throws
5,008✔
420
    }
5,008✔
421

422
    void finalize_changeset() noexcept override
423
    {
5,008✔
424
        m_history.finalize();
5,008✔
425
    }
5,008✔
426

427
    HistoryType get_history_type() const noexcept override
428
    {
20,286✔
429
        return hist_InRealm;
20,286✔
430
    }
20,286✔
431

432
    _impl::History* _get_history_write() override
433
    {
10,016✔
434
        return &m_history;
10,016✔
435
    }
10,016✔
436

437
    std::unique_ptr<_impl::History> _create_history_read() override
438
    {
74✔
439
        return std::make_unique<MyHistory>(&m_history);
74✔
440
    }
74✔
441

442
    int get_history_schema_version() const noexcept override
443
    {
38✔
444
        return 0;
38✔
445
    }
38✔
446

447
    bool is_upgradable_history_schema(int) const noexcept override
448
    {
×
449
        REALM_ASSERT(false);
×
450
        return false;
×
451
    }
×
452

453
    void upgrade_history_schema(int) override
454
    {
×
455
        REALM_ASSERT(false);
×
456
    }
×
457

458

459
private:
460
    MyHistory m_history;
461
};
462

463
} // anonymous namespace
464

465

466
TEST(LangBindHelper_AdvanceReadTransact_Basics)
467
{
2✔
468
    SHARED_GROUP_TEST_PATH(path);
2✔
469
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
470
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
471

1✔
472
    // Start a read transaction (to be repeatedly advanced)
1✔
473
    TransactionRef rt = sg->start_read();
2✔
474
    CHECK_EQUAL(0, rt->size());
2✔
475

1✔
476
    // Try to advance without anything having happened
1✔
477
    rt->advance_read();
2✔
478
    rt->verify();
2✔
479
    CHECK_EQUAL(0, rt->size());
2✔
480

1✔
481
    // Try to advance after an empty write transaction
1✔
482
    {
2✔
483
        WriteTransaction wt(sg);
2✔
484
        wt.commit();
2✔
485
    }
2✔
486
    rt->advance_read();
2✔
487
    rt->verify();
2✔
488
    CHECK_EQUAL(0, rt->size());
2✔
489

1✔
490
    // Try to advance after a superfluous rollback
1✔
491
    {
2✔
492
        WriteTransaction wt(sg);
2✔
493
        // Implicit rollback
1✔
494
    }
2✔
495
    rt->advance_read();
2✔
496
    rt->verify();
2✔
497
    CHECK_EQUAL(0, rt->size());
2✔
498

1✔
499
    // Try to advance after a propper rollback
1✔
500
    {
2✔
501
        WriteTransaction wt(sg);
2✔
502
        wt.add_table("bad");
2✔
503
        // Implicit rollback
1✔
504
    }
2✔
505
    rt->advance_read();
2✔
506
    rt->verify();
2✔
507
    CHECK_EQUAL(0, rt->size());
2✔
508

1✔
509
    // Create a table via the other SharedGroup
1✔
510
    ObjKey k0;
2✔
511
    {
2✔
512
        WriteTransaction wt(sg);
2✔
513
        TableRef foo_w = wt.add_table("foo");
2✔
514
        foo_w->add_column(type_Int, "i");
2✔
515
        k0 = foo_w->create_object().get_key();
2✔
516
        wt.commit();
2✔
517
    }
2✔
518

1✔
519
    rt->advance_read();
2✔
520
    rt->verify();
2✔
521
    CHECK_EQUAL(1, rt->size());
2✔
522
    ConstTableRef foo = rt->get_table("foo");
2✔
523
    CHECK_EQUAL(1, foo->get_column_count());
2✔
524
    auto cols = foo->get_column_keys();
2✔
525
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
526
    CHECK_EQUAL(1, foo->size());
2✔
527
    CHECK_EQUAL(0, foo->get_object(k0).get<int64_t>(cols[0]));
2✔
528
    uint_fast64_t version = foo->get_content_version();
2✔
529

1✔
530
    // Modify the table via the other SharedGroup
1✔
531
    ObjKey k1;
2✔
532
    {
2✔
533
        WriteTransaction wt(sg);
2✔
534
        TableRef foo_w = wt.get_table("foo");
2✔
535
        foo_w->add_column(type_String, "s");
2✔
536
        foo_w->add_column(type_Bool, "b");
2✔
537
        foo_w->add_column(type_Float, "f");
2✔
538
        foo_w->add_column(type_Double, "d");
2✔
539
        foo_w->add_column(type_Binary, "bin");
2✔
540
        foo_w->add_column(type_Timestamp, "t");
2✔
541
        foo_w->add_column(type_Decimal, "dec");
2✔
542
        foo_w->add_column(type_ObjectId, "oid");
2✔
543
        foo_w->add_column(*foo_w, "link");
2✔
544
        cols = foo_w->get_column_keys();
2✔
545
        auto obj1 = foo_w->create_object();
2✔
546
        auto obj0 = foo_w->get_object(k0);
2✔
547
        k1 = obj1.get_key();
2✔
548
        obj1.set_all(2, StringData("b"), true, 1.1f, 1.2, BinaryData("hopla"), Timestamp(100, 300), Decimal("100"),
2✔
549
                     ObjectId("abcdefabcdefabcdefabcdef"), k1);
2✔
550
        obj0.set<int>(cols[0], 1);
2✔
551
        obj0.set<StringData>(cols[1], "a");
2✔
552
        wt.commit();
2✔
553
    }
2✔
554
    rt->advance_read();
2✔
555
    CHECK(version != foo->get_content_version());
2✔
556
    rt->verify();
2✔
557
    cols = foo->get_column_keys();
2✔
558
    CHECK_EQUAL(10, foo->get_column_count());
2✔
559
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
560
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
561
    CHECK_EQUAL(2, foo->size());
2✔
562
    auto obj0 = foo->get_object(k0);
2✔
563
    auto obj1 = foo->get_object(k1);
2✔
564
    CHECK_EQUAL(1, obj0.get<int64_t>(cols[0]));
2✔
565
    CHECK_EQUAL(2, obj1.get<int64_t>(cols[0]));
2✔
566
    CHECK_EQUAL("a", obj0.get<StringData>(cols[1]));
2✔
567
    CHECK_EQUAL("b", obj1.get<StringData>(cols[1]));
2✔
568
    CHECK_EQUAL(obj1.get<Bool>(cols[2]), true);
2✔
569
    CHECK_EQUAL(obj1.get<float>(cols[3]), 1.1f);
2✔
570
    CHECK_EQUAL(obj1.get<double>(cols[4]), 1.2);
2✔
571
    CHECK_EQUAL(obj1.get<BinaryData>(cols[5]), BinaryData("hopla"));
2✔
572
    CHECK_EQUAL(obj1.get<Timestamp>(cols[6]), Timestamp(100, 300));
2✔
573
    CHECK_EQUAL(obj1.get<Decimal>(cols[7]), Decimal("100"));
2✔
574
    CHECK_EQUAL(obj1.get<ObjectId>(cols[8]), ObjectId("abcdefabcdefabcdefabcdef"));
2✔
575
    CHECK_EQUAL(obj1.get<ObjKey>(cols[9]), obj1.get_key());
2✔
576
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
577

1✔
578
    // Again, with no change
1✔
579
    rt->advance_read();
2✔
580
    rt->verify();
2✔
581
    CHECK_EQUAL(10, foo->get_column_count());
2✔
582
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
583
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
584
    CHECK_EQUAL(2, foo->size());
2✔
585
    CHECK_EQUAL(1, obj0.get<int64_t>(cols[0]));
2✔
586
    CHECK_EQUAL(2, obj1.get<int64_t>(cols[0]));
2✔
587
    CHECK_EQUAL("a", obj0.get<StringData>(cols[1]));
2✔
588
    CHECK_EQUAL("b", obj1.get<StringData>(cols[1]));
2✔
589
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
590

1✔
591
    // Perform several write transactions before advancing the read transaction
1✔
592
    {
2✔
593
        WriteTransaction wt(sg);
2✔
594
        TableRef bar_w = wt.add_table("bar");
2✔
595
        bar_w->add_column(type_Int, "a");
2✔
596
        wt.commit();
2✔
597
    }
2✔
598
    {
2✔
599
        WriteTransaction wt(sg);
2✔
600
        wt.commit();
2✔
601
    }
2✔
602
    {
2✔
603
        WriteTransaction wt(sg);
2✔
604
        TableRef bar_w = wt.get_table("bar");
2✔
605
        bar_w->add_column(type_Float, "b");
2✔
606
        wt.commit();
2✔
607
    }
2✔
608
    {
2✔
609
        WriteTransaction wt(sg);
2✔
610
        // Implicit rollback
1✔
611
    }
2✔
612
    {
2✔
613
        WriteTransaction wt(sg);
2✔
614
        TableRef bar_w = wt.get_table("bar");
2✔
615
        bar_w->add_column(type_Double, "c");
2✔
616
        wt.commit();
2✔
617
    }
2✔
618

1✔
619
    rt->advance_read();
2✔
620
    rt->verify();
2✔
621
    CHECK_EQUAL(2, rt->size());
2✔
622
    CHECK_EQUAL(10, foo->get_column_count());
2✔
623
    cols = foo->get_column_keys();
2✔
624
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
625
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
626
    CHECK_EQUAL(2, foo->size());
2✔
627
    CHECK_EQUAL(1, obj0.get<int64_t>(cols[0]));
2✔
628
    CHECK_EQUAL(2, obj1.get<int64_t>(cols[0]));
2✔
629
    CHECK_EQUAL("a", obj0.get<StringData>(cols[1]));
2✔
630
    CHECK_EQUAL("b", obj1.get<StringData>(cols[1]));
2✔
631
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
632
    ConstTableRef bar = rt->get_table("bar");
2✔
633
    cols = bar->get_column_keys();
2✔
634
    CHECK_EQUAL(3, bar->get_column_count());
2✔
635
    CHECK_EQUAL(type_Int, bar->get_column_type(cols[0]));
2✔
636
    CHECK_EQUAL(type_Float, bar->get_column_type(cols[1]));
2✔
637
    CHECK_EQUAL(type_Double, bar->get_column_type(cols[2]));
2✔
638

1✔
639
    // Clear tables - not supported before backlinks work again
1✔
640
    {
2✔
641
        WriteTransaction wt(sg);
2✔
642
        TableRef foo_w = wt.get_table("foo");
2✔
643
        foo_w->clear();
2✔
644
        TableRef bar_w = wt.get_table("bar");
2✔
645
        bar_w->clear();
2✔
646
        wt.commit();
2✔
647
    }
2✔
648
    rt->advance_read();
2✔
649
    rt->verify();
2✔
650

1✔
651
    size_t free_space, used_space;
2✔
652
    sg->get_stats(free_space, used_space);
2✔
653

1✔
654
    CHECK_EQUAL(2, rt->size());
2✔
655
    CHECK(foo);
2✔
656
    cols = foo->get_column_keys();
2✔
657
    CHECK_EQUAL(10, foo->get_column_count());
2✔
658
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
659
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
660
    CHECK_EQUAL(0, foo->size());
2✔
661
    CHECK(bar);
2✔
662
    cols = bar->get_column_keys();
2✔
663
    CHECK_EQUAL(3, bar->get_column_count());
2✔
664
    CHECK_EQUAL(type_Int, bar->get_column_type(cols[0]));
2✔
665
    CHECK_EQUAL(type_Float, bar->get_column_type(cols[1]));
2✔
666
    CHECK_EQUAL(type_Double, bar->get_column_type(cols[2]));
2✔
667
    CHECK_EQUAL(0, bar->size());
2✔
668
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
669
    CHECK_EQUAL(bar, rt->get_table("bar"));
2✔
670
}
2✔
671

672
TEST(LangBindHelper_AdvanceReadTransact_AddTableWithFreshSharedGroup)
673
{
2✔
674
    SHARED_GROUP_TEST_PATH(path);
2✔
675

1✔
676
    // Testing that a foreign transaction, that adds a table, can be applied to
1✔
677
    // a freshly created SharedGroup, even when another table existed in the
1✔
678
    // group prior to the one being added in the mentioned transaction. This
1✔
679
    // test is relevant because of the way table accesors are created and
1✔
680
    // managed inside a SharedGroup, in particular because table accessors are
1✔
681
    // created lazily, and will therefore not be present in a freshly created
1✔
682
    // SharedGroup instance.
1✔
683

1✔
684
    // Add the first table
1✔
685
    {
2✔
686
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
687
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
688
        WriteTransaction wt(sg_w);
2✔
689
        wt.add_table("table_1");
2✔
690
        wt.commit();
2✔
691
    }
2✔
692

1✔
693
    // Create a SharedGroup to which we can apply a foreign transaction
1✔
694
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
695
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
696
    TransactionRef rt = sg->start_read();
2✔
697

1✔
698
    // Add the second table in a "foreign" transaction
1✔
699
    {
2✔
700
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
701
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
702
        WriteTransaction wt(sg_w);
2✔
703
        wt.add_table("table_2");
2✔
704
        wt.commit();
2✔
705
    }
2✔
706

1✔
707
    rt->advance_read();
2✔
708
}
2✔
709

710

711
TEST(LangBindHelper_AdvanceReadTransact_RemoveTableWithFreshSharedGroup)
712
{
2✔
713
    SHARED_GROUP_TEST_PATH(path);
2✔
714

1✔
715
    // Testing that a foreign transaction, that removes a table, can be applied
1✔
716
    // to a freshly created Sharedrt-> This test is relevant because of the
1✔
717
    // way table accesors are created and managed inside a SharedGroup, in
1✔
718
    // particular because table accessors are created lazily, and will therefore
1✔
719
    // not be present in a freshly created SharedGroup instance.
1✔
720

1✔
721
    // Add the table
1✔
722
    {
2✔
723
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
724
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
725
        WriteTransaction wt(sg_w);
2✔
726
        wt.add_table("table");
2✔
727
        wt.commit();
2✔
728
    }
2✔
729

1✔
730
    // Create a SharedGroup to which we can apply a foreign transaction
1✔
731
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
732
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
733
    TransactionRef rt = sg->start_read();
2✔
734

1✔
735
    // remove the table in a "foreign" transaction
1✔
736
    {
2✔
737
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
738
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
739
        WriteTransaction wt(sg_w);
2✔
740
        wt.get_group().remove_table("table");
2✔
741
        wt.commit();
2✔
742
    }
2✔
743

1✔
744
    rt->advance_read();
2✔
745
}
2✔
746

747

748
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_CreateManyTables, testing_supports_spawn_process)
749
{
2✔
750
    SHARED_GROUP_TEST_PATH(path);
2✔
751
    SHARED_GROUP_TEST_PATH(path2);
2✔
752

1✔
753
    if (SpawnedProcess::is_parent()) {
2✔
754
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
755
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
756
        WriteTransaction wt(sg_w);
2✔
757
        wt.add_table("table");
2✔
758
        wt.commit();
2✔
759
    }
2✔
760

1✔
761
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
762
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
763
    TransactionRef rt = sg->start_read();
2✔
764

1✔
765
    auto process = test_util::spawn_process(test_context.test_details.test_name, "make_many_tables");
2✔
766
    if (process->is_child()) {
2✔
767
        size_t free_space, used_space;
×
768
        {
×
769
            std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
×
770
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
771

772
            WriteTransaction wt(sg_w);
×
773
            for (int i = 0; i < 16; ++i) {
×
774
                wt.add_table(util::format("table_%1", i));
×
775
            }
×
776
            wt.commit();
×
777
            sg_w->get_stats(free_space, used_space);
×
778
        }
×
779
        {
×
780
            std::unique_ptr<Replication> hist_w2(realm::make_in_realm_history());
×
781
            DBRef sg_w2 = DB::create(*hist_w2, path2, DBOptions(crypt_key()));
×
782
            WriteTransaction wt(sg_w2);
×
783
            auto table = wt.add_table("stats");
×
784
            ColKey col = table->add_column(type_Int, "used_space");
×
785
            table->create_object().set<int64_t>(col, used_space);
×
786
            wt.commit();
×
787
        }
×
788

789
        exit(0);
×
790
    }
×
791
    else {
2✔
792
        process->wait_for_child_to_finish();
2✔
793
    }
2✔
794
    size_t reported_used_space = 0;
2✔
795
    {
2✔
796
        std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
797
        DBRef sg = DB::create(*hist, path2, DBOptions(crypt_key()));
2✔
798
        WriteTransaction wt(sg);
2✔
799
        auto table = wt.get_table("stats");
2✔
800
        CHECK(table);
2✔
801
        CHECK_EQUAL(table->size(), 1);
2✔
802
        reported_used_space = size_t(table->begin()->get<int64_t>("used_space"));
2✔
803
    }
2✔
804

1✔
805
    rt->advance_read();
2✔
806
    auto used_space1 = rt->get_used_space();
2✔
807
    CHECK_EQUAL(reported_used_space, used_space1);
2✔
808
}
2✔
809

810

811
TEST(LangBindHelper_AdvanceReadTransact_PinnedSize)
812
{
2✔
813
    SHARED_GROUP_TEST_PATH(path);
2✔
814
    constexpr int num_rows = 1000;
2✔
815
    constexpr int iterations = 10;
2✔
816
    constexpr int rows_per_iteration = num_rows / iterations;
2✔
817

1✔
818
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
819
    auto sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
820
    ObjKeys keys;
2✔
821

1✔
822
    // Create some data
1✔
823
    {
2✔
824
        {
2✔
825
            WriteTransaction wt(sg);
2✔
826
            auto table = wt.add_table("table");
2✔
827
            table->add_column(type_Int, "int");
2✔
828
            wt.commit();
2✔
829
        }
2✔
830
        for (size_t i = 0; i < iterations; i++) {
22✔
831
            WriteTransaction wt(sg);
20✔
832
            auto table = wt.get_table("table");
20✔
833
            auto col = table->get_column_key("int");
20✔
834
            for (int j = 0; j < rows_per_iteration; j++) {
2,020✔
835
                auto k = table->create_object().set(col, j).get_key();
2,000✔
836
                keys.push_back(k);
2,000✔
837
            }
2,000✔
838
            wt.commit();
20✔
839
        }
20✔
840
    }
2✔
841

1✔
842
    // Pin this version
1✔
843
    auto rt = sg->start_read();
2✔
844
    size_t free_space, used_space, locked_space;
2✔
845

1✔
846
    // Make some more versions
1✔
847
    {
2✔
848
        for (int i = 0; i < iterations; i++) {
22✔
849
            WriteTransaction wt(sg);
20✔
850
            auto table = wt.get_table("table");
20✔
851
            auto col = table->get_column_key("int");
20✔
852
            for (int j = 0; j < rows_per_iteration; j++) {
2,020✔
853
                int ndx = rows_per_iteration * i + j;
2,000✔
854
                table->get_object(keys[ndx]).set(col, 2 * ndx);
2,000✔
855
            }
2,000✔
856
            wt.commit();
20✔
857
        }
20✔
858
        sg->get_stats(free_space, used_space, &locked_space);
2✔
859
    }
2✔
860

1✔
861
    CHECK_GREATER(locked_space, 0);
2✔
862
    CHECK_LESS(locked_space, free_space);
2✔
863

1✔
864
    // Cancel read transaction
1✔
865
    rt = nullptr;
2✔
866
    size_t new_locked_space;
2✔
867
    {
2✔
868
        WriteTransaction wt(sg);
2✔
869
        wt.commit();
2✔
870
        // Large history entries are freed here
1✔
871
    }
2✔
872
    {
2✔
873
        WriteTransaction wt(sg);
2✔
874
        wt.commit();
2✔
875
        // History entries still held by previous commit
1✔
876
    }
2✔
877
    {
2✔
878
        WriteTransaction wt(sg);
2✔
879
        wt.commit();
2✔
880
        // History entries now finally free
1✔
881
    }
2✔
882
    sg->get_stats(free_space, used_space, &new_locked_space);
2✔
883

1✔
884
    // Some space must have been released
1✔
885
    CHECK_LESS(new_locked_space, locked_space);
2✔
886
}
2✔
887

888

889
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_InsertTable, testing_supports_spawn_process)
890
{
2✔
891
    SHARED_GROUP_TEST_PATH(path);
2✔
892

1✔
893
    if (test_util::SpawnedProcess::is_parent()) {
2✔
894
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
895
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
896
        WriteTransaction wt(sg_w);
2✔
897

1✔
898
        TableRef table = wt.add_table("table1");
2✔
899
        table->add_column(type_Int, "col");
2✔
900

1✔
901
        table = wt.add_table("table2");
2✔
902
        table->add_column(type_Float, "col1");
2✔
903
        table->add_column(type_Float, "col2");
2✔
904

1✔
905
        wt.commit();
2✔
906
    }
2✔
907

1✔
908
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
909
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
910
    TransactionRef rt = sg->start_read();
2✔
911

1✔
912
    ConstTableRef table1 = rt->get_table("table1");
2✔
913
    ConstTableRef table2 = rt->get_table("table2");
2✔
914

1✔
915
    auto process = test_util::spawn_process(test_context.test_details.test_name, "add_table");
2✔
916
    if (process->is_child()) {
2✔
917
        {
×
918
            std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
×
919
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
920
            WriteTransaction wt(sg_w);
×
921
            wt.get_group().add_table("new table");
×
922
            wt.get_table("table1")->create_object();
×
923
            wt.get_table("table2")->create_object();
×
924
            wt.get_table("table2")->create_object();
×
925
            wt.commit();
×
926
        } // clean up sg before exit
×
927
        exit(0);
×
928
    }
×
929
    else {
2✔
930
        process->wait_for_child_to_finish();
2✔
931
    }
2✔
932

1✔
933
    rt->advance_read();
2✔
934

1✔
935
    CHECK_EQUAL(table1->size(), 1);
2✔
936
    CHECK_EQUAL(table2->size(), 2);
2✔
937
    CHECK_EQUAL(rt->get_table("new table")->size(), 0);
2✔
938
}
2✔
939

940
TEST(LangBindHelper_AdvanceReadTransact_LinkColumnInNewTable)
941
{
2✔
942
    // Verify that the table accessor of a link-opposite table is refreshed even
1✔
943
    // when the origin table is created in the same transaction as the link
1✔
944
    // column is added to it. This case is slightly involved, as there is a rule
1✔
945
    // that requires the two opposite table accessors of a link column (origin
1✔
946
    // and target sides) to either both exist or both not exist. On the other
1✔
947
    // hand, tables accessors are normally not created during
1✔
948
    // Group::advance_transact() for newly created tables.
1✔
949

1✔
950
    SHARED_GROUP_TEST_PATH(path);
2✔
951
    ShortCircuitHistory hist;
2✔
952
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
953
    DBRef sg_w = DB::create(hist, path, DBOptions(crypt_key()));
2✔
954
    {
2✔
955
        WriteTransaction wt(sg_w);
2✔
956
        wt.get_or_add_table("a");
2✔
957
        wt.commit();
2✔
958
    }
2✔
959

1✔
960
    TransactionRef rt = sg->start_read();
2✔
961
    ConstTableRef a_r = rt->get_table("a");
2✔
962

1✔
963
    {
2✔
964
        WriteTransaction wt(sg_w);
2✔
965
        TableRef a_w = wt.get_table("a");
2✔
966
        TableRef b_w = wt.get_or_add_table("b");
2✔
967
        b_w->add_column(*a_w, "foo");
2✔
968
        wt.commit();
2✔
969
    }
2✔
970

1✔
971
    rt->advance_read();
2✔
972
    CHECK(a_r);
2✔
973
    rt->verify();
2✔
974
}
2✔
975

976

977
TEST(LangBindHelper_AdvanceReadTransact_EnumeratedStrings)
978
{
2✔
979
    SHARED_GROUP_TEST_PATH(path);
2✔
980
    ShortCircuitHistory hist;
2✔
981
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
982
    ColKey c0, c1, c2;
2✔
983

1✔
984
    // Start a read transaction (to be repeatedly advanced)
1✔
985
    auto rt = sg->start_read();
2✔
986
    CHECK_EQUAL(0, rt->size());
2✔
987

1✔
988
    // Create 3 string columns, one primed for conversion to "unique string
1✔
989
    // enumeration" representation
1✔
990
    {
2✔
991
        WriteTransaction wt(sg);
2✔
992
        TableRef table_w = wt.add_table("t");
2✔
993
        c0 = table_w->add_column(type_String, "a");
2✔
994
        c1 = table_w->add_column(type_String, "b");
2✔
995
        c2 = table_w->add_column(type_String, "c");
2✔
996
        for (int i = 0; i < 1000; ++i) {
2,002✔
997
            std::ostringstream out;
2,000✔
998
            out << i;
2,000✔
999
            std::string str = out.str();
2,000✔
1000
            table_w->create_object(ObjKey{}, {{c0, str}, {c1, "foo"}, {c2, str}});
2,000✔
1001
        }
2,000✔
1002
        wt.commit();
2✔
1003
    }
2✔
1004
    rt->advance_read();
2✔
1005
    rt->verify();
2✔
1006
    ConstTableRef table = rt->get_table("t");
2✔
1007
    CHECK_EQUAL(0, table->get_num_unique_values(c0));
2✔
1008
    CHECK_EQUAL(0, table->get_num_unique_values(c1)); // Not yet "optimized"
2✔
1009
    CHECK_EQUAL(0, table->get_num_unique_values(c2));
2✔
1010

1✔
1011
    // Optimize
1✔
1012
    {
2✔
1013
        WriteTransaction wt(sg);
2✔
1014
        TableRef table_w = wt.get_table("t");
2✔
1015
        table_w->enumerate_string_column(c1);
2✔
1016
        wt.commit();
2✔
1017
    }
2✔
1018
    rt->advance_read();
2✔
1019
    rt->verify();
2✔
1020
    CHECK_EQUAL(0, table->get_num_unique_values(c0));
2✔
1021
    CHECK_NOT_EQUAL(0, table->get_num_unique_values(c1)); // Must be "optimized" now
2✔
1022
    CHECK_EQUAL(0, table->get_num_unique_values(c2));
2✔
1023
}
2✔
1024

1025
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_SearchIndex, testing_supports_spawn_process)
1026
{
2✔
1027
    SHARED_GROUP_TEST_PATH(path);
2✔
1028
    if (test_util::SpawnedProcess::is_parent()) {
2✔
1029
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1030
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1031

1✔
1032
        // Start a read transaction (to be repeatedly advanced)
1✔
1033
        TransactionRef rt = sg->start_read();
2✔
1034
        CHECK_EQUAL(0, rt->size());
2✔
1035
    }
2✔
1036
    // Create 5 columns, and make 3 of them indexed
1✔
1037
    auto process = test_util::spawn_process(test_context.test_details.test_name, "init");
2✔
1038
    if (process->is_child()) {
2✔
1039
        {
×
1040
            std::vector<ObjKey> keys;
×
1041
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1042
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1043
            WriteTransaction wt(sg_w);
×
1044
            TableRef table_w = wt.add_table("t");
×
1045
            ColKey col_int = table_w->add_column(type_Int, "i0");
×
1046
            table_w->add_column(type_String, "s1");
×
1047
            ColKey col_str2 = table_w->add_column(type_String, "s2");
×
1048
            table_w->add_column(type_Int, "i3");
×
1049
            ColKey col_int4 = table_w->add_column(type_Int, "i4");
×
1050
            table_w->add_search_index(col_int);
×
1051
            table_w->add_search_index(col_str2);
×
1052
            table_w->add_search_index(col_int4);
×
1053
            table_w->create_objects(8, keys);
×
1054
            wt.commit();
×
1055
        } // clean up sg before exit
×
1056
        exit(0);
×
1057
    }
×
1058

1✔
1059
    if (process->is_parent()) {
2✔
1060
        process->wait_for_child_to_finish();
2✔
1061

1✔
1062
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1063
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1064

1✔
1065
        // Start a read transaction (to be repeatedly advanced)
1✔
1066
        TransactionRef rt = sg->start_read();
2✔
1067
        rt->advance_read();
2✔
1068
        rt->verify();
2✔
1069
        ConstTableRef table = rt->get_table("t");
2✔
1070
        CHECK(table->has_search_index(table->get_column_key("i0")));
2✔
1071
        CHECK_NOT(table->has_search_index(table->get_column_key("s1")));
2✔
1072
        CHECK(table->has_search_index(table->get_column_key("s2")));
2✔
1073
        CHECK_NOT(table->has_search_index(table->get_column_key("i3")));
2✔
1074
        CHECK(table->has_search_index(table->get_column_key("i4")));
2✔
1075
    }
2✔
1076

1✔
1077
    // Remove the previous search indexes and add 2 new ones
1✔
1078
    process = test_util::spawn_process(test_context.test_details.test_name, "change_indexes");
2✔
1079
    if (process->is_child()) {
2✔
1080
        {
×
1081
            std::vector<ObjKey> keys;
×
1082
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1083
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1084
            WriteTransaction wt(sg_w);
×
1085
            TableRef table_w = wt.get_table("t");
×
1086
            table_w->create_objects(8, keys);
×
1087
            table_w->remove_search_index(table_w->get_column_key("s2"));
×
1088
            table_w->add_search_index(table_w->get_column_key("i3"));
×
1089
            table_w->remove_search_index(table_w->get_column_key("i0"));
×
1090
            table_w->add_search_index(table_w->get_column_key("s1"));
×
1091
            table_w->remove_search_index(table_w->get_column_key("i4"));
×
1092
            wt.commit();
×
1093
        }
×
1094
        exit(0);
×
1095
    }
×
1096

1✔
1097
    if (process->is_parent()) {
2✔
1098
        process->wait_for_child_to_finish();
2✔
1099

1✔
1100
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1101
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1102

1✔
1103
        // Start a read transaction (to be repeatedly advanced)
1✔
1104
        TransactionRef rt = sg->start_read();
2✔
1105
        ConstTableRef table = rt->get_table("t");
2✔
1106
        rt->advance_read();
2✔
1107
        rt->verify();
2✔
1108
        CHECK_NOT(table->has_search_index(table->get_column_key("i0")));
2✔
1109
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1110
        CHECK_NOT(table->has_search_index(table->get_column_key("s2")));
2✔
1111
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1112
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1113
    }
2✔
1114

1✔
1115
    // Add some searchable contents
1✔
1116
    process = test_util::spawn_process(test_context.test_details.test_name, "add_content");
2✔
1117
    if (process->is_child()) {
2✔
1118
        {
×
1119
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1120
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1121
            WriteTransaction wt(sg_w);
×
1122
            TableRef table_w = wt.get_table("t");
×
1123
            int_fast64_t v = 7;
×
1124
            for (auto obj : *table_w) {
×
1125
                std::string out(util::to_string(v));
×
1126
                obj.set(table_w->get_column_key("s1"), StringData(out));
×
1127
                obj.set(table_w->get_column_key("i3"), v);
×
1128
                v = (v + 1581757577LL) % 1000;
×
1129
            }
×
1130
            wt.commit();
×
1131
        }
×
1132
        exit(0);
×
1133
    }
×
1134
    if (process->is_parent()) {
2✔
1135
        process->wait_for_child_to_finish();
2✔
1136

1✔
1137
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1138
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1139

1✔
1140
        // Start a read transaction (to be repeatedly advanced)
1✔
1141
        TransactionRef rt = sg->start_read();
2✔
1142
        ConstTableRef table = rt->get_table("t");
2✔
1143
        rt->advance_read();
2✔
1144
        rt->verify();
2✔
1145

1✔
1146
        CHECK_NOT(table->has_search_index(table->get_column_key("i0")));
2✔
1147
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1148
        CHECK_NOT(table->has_search_index(table->get_column_key("s2")));
2✔
1149
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1150
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1151
        CHECK_EQUAL(ObjKey(12), table->find_first_string(table->get_column_key("s1"), "931"));
2✔
1152
        CHECK_EQUAL(ObjKey(4), table->find_first_int(table->get_column_key("i3"), 315));
2✔
1153
        CHECK_EQUAL(ObjKey(13), table->find_first_int(table->get_column_key("i3"), 508));
2✔
1154
    }
2✔
1155
    // Move the indexed columns by removal
1✔
1156
    process = test_util::spawn_process(test_context.test_details.test_name, "move_and_remove");
2✔
1157
    if (process->is_child()) {
2✔
1158
        {
×
1159
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1160
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1161
            WriteTransaction wt(sg_w);
×
1162
            TableRef table_w = wt.get_table("t");
×
1163
            table_w->remove_column(table_w->get_column_key("i0"));
×
1164
            table_w->remove_column(table_w->get_column_key("s2"));
×
1165
            wt.commit();
×
1166
        }
×
1167
        exit(0);
×
1168
    }
×
1169
    if (process->is_parent()) {
2✔
1170
        process->wait_for_child_to_finish();
2✔
1171

1✔
1172
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1173
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1174

1✔
1175
        // Start a read transaction (to be repeatedly advanced)
1✔
1176
        TransactionRef rt = sg->start_read();
2✔
1177
        ConstTableRef table = rt->get_table("t");
2✔
1178
        rt->advance_read();
2✔
1179
        rt->verify();
2✔
1180
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1181
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1182
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1183
        CHECK_EQUAL(ObjKey(3), table->find_first_string(table->get_column_key("s1"), "738"));
2✔
1184
        CHECK_EQUAL(ObjKey(13), table->find_first_int(table->get_column_key("i3"), 508));
2✔
1185
    }
2✔
1186
}
2✔
1187

1188
TEST(LangBindHelper_AdvanceReadTransact_LinkView)
1189
{
2✔
1190
    SHARED_GROUP_TEST_PATH(path);
2✔
1191
    ShortCircuitHistory hist;
2✔
1192
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1193
    DBRef sg_w = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1194
    DBRef sg_q = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1195

1✔
1196
    // Start a continuous read transaction
1✔
1197
    TransactionRef rt = sg->start_read();
2✔
1198

1✔
1199
    // Add some tables and rows.
1✔
1200
    {
2✔
1201
        WriteTransaction wt(sg_w);
2✔
1202
        TableRef origin = wt.add_table("origin");
2✔
1203
        TableRef target = wt.add_table("target");
2✔
1204
        target->add_column(type_Int, "value");
2✔
1205
        auto col = origin->add_column_list(*target, "list");
2✔
1206

1✔
1207
        std::vector<ObjKey> keys;
2✔
1208
        target->create_objects(10, keys);
2✔
1209

1✔
1210
        Obj o0 = origin->create_object(ObjKey(0));
2✔
1211
        Obj o1 = origin->create_object(ObjKey(1));
2✔
1212

1✔
1213
        o0.get_linklist(col).add(keys[1]);
2✔
1214
        o1.get_linklist(col).add(keys[2]);
2✔
1215
        // state:
1✔
1216
        // origin[0].ll[0] -> target[1]
1✔
1217
        // origin[1].ll[0] -> target[2]
1✔
1218
        wt.commit();
2✔
1219
    }
2✔
1220
    rt->advance_read();
2✔
1221
    rt->verify();
2✔
1222

1✔
1223
    // Grab references to the LinkViews
1✔
1224
    auto origin = rt->get_table("origin");
2✔
1225
    auto col_link = origin->get_column_key("list");
2✔
1226
    const Obj obj0 = origin->get_object(ObjKey(0));
2✔
1227
    const Obj obj1 = origin->get_object(ObjKey(1));
2✔
1228

1✔
1229
    auto ll1 = obj0.get_linklist(col_link); // lv1[0] -> target[1]
2✔
1230
    auto ll2 = obj1.get_linklist(col_link); // lv2[0] -> target[2]
2✔
1231
    CHECK_EQUAL(ll1.size(), 1);
2✔
1232
    CHECK_EQUAL(ll2.size(), 1);
2✔
1233

1✔
1234
    ObjKey ll1_target = ll1.get_object(0).get_key();
2✔
1235
    CHECK_EQUAL(ll1.find_first(ll1_target), 0);
2✔
1236

1✔
1237
    {
2✔
1238
        WriteTransaction wt(sg_w);
2✔
1239
        wt.get_table("origin")->get_object(ObjKey(0)).get_linklist(col_link).clear();
2✔
1240
        wt.commit();
2✔
1241
    }
2✔
1242
    rt->advance_read();
2✔
1243
    rt->verify();
2✔
1244

1✔
1245
    CHECK_EQUAL(ll1.find_first(ll1_target), not_found);
2✔
1246
}
2✔
1247

1248
namespace {
1249

1250
template <typename T>
1251
class ConcurrentQueue {
1252
public:
1253
    ConcurrentQueue(size_t size)
1254
        : sz(size)
1255
    {
2✔
1256
        data.reset(new T[sz]);
2✔
1257
    }
2✔
1258
    inline bool is_full()
1259
    {
199,959✔
1260
        return writer - reader == sz;
199,959✔
1261
    }
199,959✔
1262
    inline bool is_empty()
1263
    {
212,152✔
1264
        return writer - reader == 0;
212,152✔
1265
    }
212,152✔
1266
    void put(T& e)
1267
    {
100,000✔
1268
        std::unique_lock<std::mutex> lock(mutex);
100,000✔
1269
        while (is_full())
100,000✔
UNCOV
1270
            not_full.wait(lock);
×
1271
        if (is_empty())
100,000✔
1272
            not_empty_or_closed.notify_all();
20,324✔
1273
        data[writer++ % sz] = std::move(e);
100,000✔
1274
    }
100,000✔
1275

1276
    bool get(T& e)
1277
    {
99,961✔
1278
        std::unique_lock<std::mutex> lock(mutex);
99,961✔
1279
        while (is_empty() && !closed)
112,152✔
1280
            not_empty_or_closed.wait(lock);
12,191✔
1281
        if (closed)
99,961✔
1282
            return false;
2✔
1283
        if (is_full())
99,959✔
UNCOV
1284
            not_full.notify_all();
×
1285
        e = std::move(data[reader++ % sz]);
99,959✔
1286
        return true;
99,959✔
1287
    }
99,959✔
1288

1289
    void reopen()
1290
    {
1291
        // no concurrent access allowed here
1292
        closed = false;
1293
    }
1294

1295
    void close()
1296
    {
2✔
1297
        std::unique_lock<std::mutex> lock(mutex);
2✔
1298
        closed = true;
2✔
1299
        not_empty_or_closed.notify_all();
2✔
1300
    }
2✔
1301

1302
private:
1303
    std::mutex mutex;
1304
    std::condition_variable not_full;
1305
    std::condition_variable not_empty_or_closed;
1306
    size_t reader = 0;
1307
    size_t writer = 0;
1308
    bool closed = false;
1309
    size_t sz;
1310
    std::unique_ptr<T[]> data;
1311
};
1312

1313
// Background thread for test below.
1314
void deleter_thread(ConcurrentQueue<LnkLstPtr>& queue)
1315
{
2✔
1316
    Random random(random_int<unsigned long>());
2✔
1317
    bool closed = false;
2✔
1318
    while (!closed) {
99,963✔
1319
        LnkLstPtr r;
99,961✔
1320
        // prevent the compiler from eliminating a loop:
49,992✔
1321
        volatile int delay = random.draw_int_mod(10000);
99,961✔
1322
        closed = !queue.get(r);
99,961✔
1323
        // random delay goes *after* get(), so that it comes
49,992✔
1324
        // after the potentially synchronizing locking
49,992✔
1325
        // operation inside queue.get()
49,992✔
1326
        while (delay > 0)
500,485,022✔
1327
            delay = delay - 1;
500,385,061✔
1328
        // just let 'r' die
49,992✔
1329
    }
99,961✔
1330
}
2✔
1331
} // namespace
1332

1333
TEST(LangBindHelper_ConcurrentLinkViewDeletes)
1334
{
2✔
1335
    // This tests checks concurrent deletion of LinkViews.
1✔
1336
    // It is structured as a mutator which creates and uses
1✔
1337
    // LinkView accessors, and a background deleter which
1✔
1338
    // consumes LinkViewRefs and makes them go out of scope
1✔
1339
    // concurrently with the new references being created.
1✔
1340

1✔
1341
    // Number of table entries (and hence, max number of accessors)
1✔
1342
    const int table_size = 1000;
2✔
1343

1✔
1344
    // Number of references produced (some will refer to the same
1✔
1345
    // accessor)
1✔
1346
    const int max_refs = 50000;
2✔
1347

1✔
1348
    // Frequency of references that are used to change the
1✔
1349
    // database during the test.
1✔
1350
    const int change_frequency_per_mill = 50000; // 5pct changes
2✔
1351

1✔
1352
    // Number of references that may be buffered for communication
1✔
1353
    // between main thread and deleter thread. Should be large enough
1✔
1354
    // to allow considerable overlap.
1✔
1355
    const int buffer_size = 2000;
2✔
1356

1✔
1357
    Random random(random_int<unsigned long>());
2✔
1358

1✔
1359
    // setup two tables with empty linklists inside
1✔
1360
    SHARED_GROUP_TEST_PATH(path);
2✔
1361
    ShortCircuitHistory hist;
2✔
1362
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1363

1✔
1364
    // Start a read transaction (to be repeatedly advanced)
1✔
1365
    std::vector<ObjKey> o_keys;
2✔
1366
    std::vector<ObjKey> t_keys;
2✔
1367
    ColKey ck;
2✔
1368
    auto rt = sg->start_read();
2✔
1369
    {
2✔
1370
        // setup tables with empty linklists
1✔
1371
        WriteTransaction wt(sg);
2✔
1372
        TableRef origin = wt.add_table("origin");
2✔
1373
        TableRef target = wt.add_table("target");
2✔
1374
        ck = origin->add_column_list(*target, "ll");
2✔
1375
        origin->create_objects(table_size, o_keys);
2✔
1376
        target->create_objects(table_size, t_keys);
2✔
1377
        wt.commit();
2✔
1378
    }
2✔
1379
    rt->advance_read();
2✔
1380

1✔
1381
    // Create accessors for random entries in the table.
1✔
1382
    // occasionally modify the database through the accessor.
1✔
1383
    // feed the accessor refs to the background thread for
1✔
1384
    // later deletion.
1✔
1385
    util::Thread deleter;
2✔
1386
    ConcurrentQueue<LnkLstPtr> queue(buffer_size);
2✔
1387
    deleter.start([&] {
2✔
1388
        deleter_thread(queue);
2✔
1389
    });
2✔
1390
    for (int i = 0; i < max_refs; ++i) {
100,002✔
1391
        TableRef origin = rt->get_table("origin");
100,000✔
1392
        int ndx = random.draw_int_mod(table_size);
100,000✔
1393
        Obj o = origin->get_object(o_keys[ndx]);
100,000✔
1394
        LnkLstPtr lw = o.get_linklist_ptr(ck);
100,000✔
1395
        bool will_add = change_frequency_per_mill > random.draw_int_mod(1000000);
100,000✔
1396
        if (will_add) {
100,000✔
1397
            rt->promote_to_write();
4,924✔
1398
            lw->add(t_keys[ndx]);
4,924✔
1399
            rt->commit_and_continue_as_read();
4,924✔
1400
        }
4,924✔
1401
        queue.put(lw);
100,000✔
1402
    }
100,000✔
1403
    queue.close();
2✔
1404
    deleter.join();
2✔
1405
}
2✔
1406

1407
TEST(LangBindHelper_AdvanceReadTransact_InsertLink)
1408
{
2✔
1409
    // This test checks that Table::insert_link() works across transaction
1✔
1410
    // boundaries (advance transaction).
1✔
1411

1✔
1412
    SHARED_GROUP_TEST_PATH(path);
2✔
1413
    ShortCircuitHistory hist;
2✔
1414
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1415

1✔
1416
    // Start a read transaction (to be repeatedly advanced)
1✔
1417
    TransactionRef rt = sg->start_read();
2✔
1418
    CHECK_EQUAL(0, rt->size());
2✔
1419
    ColKey col;
2✔
1420
    ObjKey target_key;
2✔
1421
    {
2✔
1422
        WriteTransaction wt(sg);
2✔
1423
        TableRef origin_w = wt.add_table("origin");
2✔
1424
        TableRef target_w = wt.add_table("target");
2✔
1425
        col = origin_w->add_column(*target_w, "");
2✔
1426
        target_w->add_column(type_Int, "");
2✔
1427
        target_key = target_w->create_object().get_key();
2✔
1428
        wt.commit();
2✔
1429
    }
2✔
1430
    rt->advance_read();
2✔
1431
    rt->verify();
2✔
1432
    ConstTableRef origin = rt->get_table("origin");
2✔
1433
    ConstTableRef target = rt->get_table("target");
2✔
1434
    {
2✔
1435
        WriteTransaction wt(sg);
2✔
1436
        TableRef origin_w = wt.get_table("origin");
2✔
1437
        auto obj = origin_w->create_object();
2✔
1438
        obj.set(col, target_key);
2✔
1439
        wt.commit();
2✔
1440
    }
2✔
1441
    rt->advance_read();
2✔
1442
    CHECK(origin);
2✔
1443
    CHECK(target);
2✔
1444
    rt->verify();
2✔
1445
}
2✔
1446

1447

1448
TEST(LangBindHelper_AdvanceReadTransact_LinkToNeighbour)
1449
{
2✔
1450
    // This test checks that you can insert a link to an object that resides
1✔
1451
    // in the same cluster as the origin object.
1✔
1452

1✔
1453
    SHARED_GROUP_TEST_PATH(path);
2✔
1454
    ShortCircuitHistory hist;
2✔
1455
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1456

1✔
1457
    // Start a read transaction (to be repeatedly advanced)
1✔
1458
    TransactionRef rt = sg->start_read();
2✔
1459
    CHECK_EQUAL(0, rt->size());
2✔
1460
    ColKey col;
2✔
1461
    std::vector<ObjKey> keys;
2✔
1462
    {
2✔
1463
        WriteTransaction wt(sg);
2✔
1464
        TableRef table = wt.add_table("table");
2✔
1465
        table->add_column(type_Int, "integers");
2✔
1466
        col = table->add_column(*table, "links");
2✔
1467
        table->create_objects(10, keys);
2✔
1468
        wt.commit();
2✔
1469
    }
2✔
1470
    rt->advance_read();
2✔
1471
    rt->verify();
2✔
1472
    {
2✔
1473
        WriteTransaction wt(sg);
2✔
1474
        TableRef table = wt.get_table("table");
2✔
1475
        table->get_object(keys[0]).set(col, keys[1]);
2✔
1476
        table->get_object(keys[1]).set(col, keys[2]);
2✔
1477
        wt.commit();
2✔
1478
    }
2✔
1479
    rt->advance_read();
2✔
1480
    rt->verify();
2✔
1481
}
2✔
1482

1483
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_RemoveTableWithColumns, testing_supports_spawn_process)
1484
{
2✔
1485
    SHARED_GROUP_TEST_PATH(path);
2✔
1486
    if (test_util::SpawnedProcess::is_parent()) {
2✔
1487
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1488
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1489

1✔
1490
        // Start a read transaction (to be repeatedly advanced)
1✔
1491
        TransactionRef rt = sg->start_read();
2✔
1492
        CHECK_EQUAL(0, rt->size());
2✔
1493
    }
2✔
1494
    auto process = test_util::spawn_process(test_context.test_details.test_name, "initial_write");
2✔
1495
    if (process->is_child()) {
2✔
1496
        {
×
1497
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1498
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1499
            WriteTransaction wt(sg_w);
×
1500
            TableRef alpha_w = wt.add_table("alpha");
×
1501
            TableRef beta_w = wt.add_table("beta");
×
1502
            TableRef gamma_w = wt.add_table("gamma");
×
1503
            TableRef delta_w = wt.add_table("delta");
×
1504
            TableRef epsilon_w = wt.add_table("epsilon");
×
1505
            alpha_w->add_column(type_Int, "alpha-1");
×
1506
            beta_w->add_column(*delta_w, "beta-1");
×
1507
            gamma_w->add_column(*gamma_w, "gamma-1");
×
1508
            delta_w->add_column(type_Int, "delta-1");
×
1509
            epsilon_w->add_column(*delta_w, "epsilon-1");
×
1510
            wt.commit();
×
1511
        } // clean up sg before exit
×
1512
        exit(0);
×
1513
    }
×
1514
    else if (process->is_parent()) {
2✔
1515
        process->wait_for_child_to_finish();
2✔
1516

1✔
1517
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1518
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1519

1✔
1520
        // Start a read transaction (to be repeatedly advanced)
1✔
1521
        TransactionRef rt = sg->start_read();
2✔
1522
        rt->advance_read();
2✔
1523
        rt->verify();
2✔
1524

1✔
1525
        CHECK_EQUAL(5, rt->size());
2✔
1526
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1527
        ConstTableRef beta = rt->get_table("beta");
2✔
1528
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1529
        ConstTableRef delta = rt->get_table("delta");
2✔
1530
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1531
        CHECK(alpha);
2✔
1532
        CHECK(beta);
2✔
1533
        CHECK(gamma);
2✔
1534
        CHECK(delta);
2✔
1535
        CHECK(epsilon);
2✔
1536
    }
2✔
1537
    // Remove table with columns, but no link columns, and table is not a link
1✔
1538
    // target.
1✔
1539
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_alpha");
2✔
1540
    if (process->is_child()) {
2✔
1541
        {
×
1542
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1543
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1544
            WriteTransaction wt(sg_w);
×
1545
            wt.get_group().remove_table("alpha");
×
1546
            wt.commit();
×
1547
        }
×
1548
        exit(0);
×
1549
    }
×
1550
    else if (process->is_parent()) {
2✔
1551
        process->wait_for_child_to_finish();
2✔
1552

1✔
1553
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1554
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1555

1✔
1556
        // Start a read transaction (to be repeatedly advanced)
1✔
1557
        TransactionRef rt = sg->start_read();
2✔
1558
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1559
        ConstTableRef beta = rt->get_table("beta");
2✔
1560
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1561
        ConstTableRef delta = rt->get_table("delta");
2✔
1562
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1563
        rt->advance_read();
2✔
1564
        rt->verify();
2✔
1565

1✔
1566
        CHECK_EQUAL(4, rt->size());
2✔
1567
        CHECK_NOT(alpha);
2✔
1568
        CHECK(beta);
2✔
1569
        CHECK(gamma);
2✔
1570
        CHECK(delta);
2✔
1571
        CHECK(epsilon);
2✔
1572
    }
2✔
1573
    // Remove table with link column, and table is not a link target.
1✔
1574
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_beta");
2✔
1575
    if (process->is_child()) {
2✔
1576
        {
×
1577
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1578
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1579
            WriteTransaction wt(sg_w);
×
1580
            wt.get_group().remove_table("beta");
×
1581
            wt.commit();
×
1582
        }
×
1583
        exit(0);
×
1584
    }
×
1585
    else if (process->is_parent()) {
2✔
1586
        process->wait_for_child_to_finish();
2✔
1587

1✔
1588
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1589
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1590

1✔
1591
        // Start a read transaction (to be repeatedly advanced)
1✔
1592
        TransactionRef rt = sg->start_read();
2✔
1593
        rt->advance_read();
2✔
1594
        rt->verify();
2✔
1595

1✔
1596
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1597
        ConstTableRef beta = rt->get_table("beta");
2✔
1598
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1599
        ConstTableRef delta = rt->get_table("delta");
2✔
1600
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1601
        CHECK_EQUAL(3, rt->size());
2✔
1602
        CHECK_NOT(alpha);
2✔
1603
        CHECK_NOT(beta);
2✔
1604
        CHECK(gamma);
2✔
1605
        CHECK(delta);
2✔
1606
        CHECK(epsilon);
2✔
1607
    }
2✔
1608
    // Remove table with self-link column, and table is not a target of link
1✔
1609
    // columns of other tables.
1✔
1610
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_gamma");
2✔
1611
    if (process->is_child()) {
2✔
1612
        {
×
1613
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1614
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1615
            WriteTransaction wt(sg_w);
×
1616
            wt.get_group().remove_table("gamma");
×
1617
            wt.commit();
×
1618
        }
×
1619
        exit(0);
×
1620
    }
×
1621
    else if (process->is_parent()) {
2✔
1622
        process->wait_for_child_to_finish();
2✔
1623

1✔
1624
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1625
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1626

1✔
1627
        // Start a read transaction (to be repeatedly advanced)
1✔
1628
        TransactionRef rt = sg->start_read();
2✔
1629
        rt->advance_read();
2✔
1630
        rt->verify();
2✔
1631

1✔
1632
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1633
        ConstTableRef beta = rt->get_table("beta");
2✔
1634
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1635
        ConstTableRef delta = rt->get_table("delta");
2✔
1636
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1637
        CHECK_EQUAL(2, rt->size());
2✔
1638
        CHECK_NOT(alpha);
2✔
1639
        CHECK_NOT(beta);
2✔
1640
        CHECK_NOT(gamma);
2✔
1641
        CHECK(delta);
2✔
1642
        CHECK(epsilon);
2✔
1643
    }
2✔
1644
    // Try, but fail to remove table which is a target of link columns of other
1✔
1645
    // tables.
1✔
1646
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_delta");
2✔
1647
    if (process->is_child()) {
2✔
1648
        {
×
1649
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1650
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1651
            WriteTransaction wt(sg_w);
×
1652
            CHECK_THROW(wt.get_group().remove_table("delta"), CrossTableLinkTarget);
×
1653
            wt.commit();
×
1654
        }
×
1655
        exit(0);
×
1656
    }
×
1657
    else if (process->is_parent()) {
2✔
1658
        process->wait_for_child_to_finish();
2✔
1659

1✔
1660
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1661
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1662

1✔
1663
        // Start a read transaction (to be repeatedly advanced)
1✔
1664
        TransactionRef rt = sg->start_read();
2✔
1665
        rt->advance_read();
2✔
1666
        rt->verify();
2✔
1667
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1668
        ConstTableRef beta = rt->get_table("beta");
2✔
1669
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1670
        ConstTableRef delta = rt->get_table("delta");
2✔
1671
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1672

1✔
1673
        CHECK_EQUAL(2, rt->size());
2✔
1674
        CHECK_NOT(alpha);
2✔
1675
        CHECK_NOT(beta);
2✔
1676
        CHECK_NOT(gamma);
2✔
1677
        CHECK(delta);
2✔
1678
        CHECK(epsilon);
2✔
1679
    }
2✔
1680
}
2✔
1681

1682
TEST(LangBindHelper_AdvanceReadTransact_CascadeRemove_ColumnLink)
1683
{
2✔
1684
    SHARED_GROUP_TEST_PATH(path);
2✔
1685
    ShortCircuitHistory hist;
2✔
1686
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1687

1✔
1688
    ColKey col;
2✔
1689
    {
2✔
1690
        WriteTransaction wt(sg);
2✔
1691
        auto origin = wt.add_table("origin");
2✔
1692
        auto target = wt.add_table("target", Table::Type::Embedded);
2✔
1693
        col = origin->add_column(*target, "o_1");
2✔
1694
        target->add_column(type_Int, "t_1");
2✔
1695
        wt.commit();
2✔
1696
    }
2✔
1697

1✔
1698
    // Start a read transaction (to be repeatedly advanced)
1✔
1699
    auto rt = sg->start_read();
2✔
1700
    auto target = rt->get_table("target");
2✔
1701

1✔
1702
    ObjKey target_key0, target_key1;
2✔
1703
    Obj target_obj0, target_obj1;
2✔
1704

1✔
1705
    auto perform_change = [&](util::FunctionRef<void(Table&)> func) {
6✔
1706
        // Ensure there are two rows in each table, with each row in `origin`
3✔
1707
        // pointing to the corresponding row in `target`
3✔
1708
        {
6✔
1709
            WriteTransaction wt(sg);
6✔
1710
            auto origin_w = wt.get_table("origin");
6✔
1711
            auto target_w = wt.get_table("target");
6✔
1712

3✔
1713
            origin_w->clear();
6✔
1714
            target_w->clear();
6✔
1715
            auto o0 = origin_w->create_object();
6✔
1716
            auto o1 = origin_w->create_object();
6✔
1717
            target_key0 = o0.create_and_set_linked_object(col).get_key();
6✔
1718
            target_key1 = o1.create_and_set_linked_object(col).get_key();
6✔
1719
            wt.commit();
6✔
1720
        }
6✔
1721

3✔
1722
        // Grab the row accessors before applying the modification being tested
3✔
1723
        rt->advance_read();
6✔
1724
        rt->verify();
6✔
1725
        target_obj0 = target->get_object(target_key0);
6✔
1726
        target_obj1 = target->get_object(target_key1);
6✔
1727

3✔
1728
        // Perform the modification
3✔
1729
        {
6✔
1730
            WriteTransaction wt(sg);
6✔
1731
            func(*wt.get_table("origin"));
6✔
1732
            wt.commit();
6✔
1733
        }
6✔
1734

3✔
1735
        rt->advance_read();
6✔
1736
        rt->verify();
6✔
1737
        // Leave `group` and the target accessors in a state which can be tested
3✔
1738
        // with the changes applied
3✔
1739
    };
6✔
1740

1✔
1741
    // Break link by clearing table
1✔
1742
    perform_change([](Table& origin) {
2✔
1743
        origin.clear();
2✔
1744
    });
2✔
1745
    CHECK(!target_obj0.is_valid());
2✔
1746
    CHECK(!target_obj1.is_valid());
2✔
1747
    CHECK_EQUAL(target->size(), 0);
2✔
1748

1✔
1749
    // Break link by nullifying
1✔
1750
    perform_change([&](Table& origin) {
2✔
1751
        origin.get_object(1).set_null(col);
2✔
1752
    });
2✔
1753
    CHECK(target_obj0.is_valid());
2✔
1754
    CHECK(!target_obj1.is_valid());
2✔
1755
    CHECK_EQUAL(target->size(), 1);
2✔
1756

1✔
1757
    // Break link by reassign
1✔
1758
    perform_change([&](Table& origin) {
2✔
1759
        origin.get_object(1).create_and_set_linked_object(col);
2✔
1760
    });
2✔
1761
    CHECK(target_obj0.is_valid());
2✔
1762
    CHECK(!target_obj1.is_valid());
2✔
1763
    CHECK_EQUAL(target->size(), 2);
2✔
1764
}
2✔
1765

1766

1767
TEST(LangBindHelper_AdvanceReadTransact_CascadeRemove_ColumnLinkList)
1768
{
2✔
1769
    SHARED_GROUP_TEST_PATH(path);
2✔
1770
    ShortCircuitHistory hist;
2✔
1771
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1772

1✔
1773
    ColKey col;
2✔
1774
    {
2✔
1775
        WriteTransaction wt(sg);
2✔
1776
        auto origin = wt.add_table("origin");
2✔
1777
        auto target = wt.add_table("target", Table::Type::Embedded);
2✔
1778
        col = origin->add_column_list(*target, "o_1");
2✔
1779
        target->add_column(type_Int, "t_1");
2✔
1780
        wt.commit();
2✔
1781
    }
2✔
1782

1✔
1783
    // Start a read transaction (to be repeatedly advanced)
1✔
1784
    auto rt = sg->start_read();
2✔
1785
    auto target = rt->get_table("target");
2✔
1786

1✔
1787
    ObjKey target_key0, target_key1;
2✔
1788
    Obj target_obj0, target_obj1;
2✔
1789

1✔
1790
    auto perform_change = [&](util::FunctionRef<void(Table&)> func) {
8✔
1791
        // Ensure there are two rows in each table, with each row in `origin`
4✔
1792
        // pointing to the corresponding row in `target`
4✔
1793
        {
8✔
1794
            WriteTransaction wt(sg);
8✔
1795
            auto origin_w = wt.get_table("origin");
8✔
1796
            auto target_w = wt.get_table("target");
8✔
1797

4✔
1798
            origin_w->clear();
8✔
1799
            target_w->clear();
8✔
1800
            auto o0 = origin_w->create_object();
8✔
1801
            auto o1 = origin_w->create_object();
8✔
1802
            target_key0 = o0.get_linklist(col).create_and_insert_linked_object(0).get_key();
8✔
1803
            target_key1 = o1.get_linklist(col).create_and_insert_linked_object(0).get_key();
8✔
1804
            wt.commit();
8✔
1805
        }
8✔
1806

4✔
1807
        // Grab the row accessors before applying the modification being tested
4✔
1808
        rt->advance_read();
8✔
1809
        rt->verify();
8✔
1810
        target_obj0 = target->get_object(target_key0);
8✔
1811
        target_obj1 = target->get_object(target_key1);
8✔
1812

4✔
1813
        // Perform the modification
4✔
1814
        {
8✔
1815
            WriteTransaction wt(sg);
8✔
1816
            func(*wt.get_table("origin"));
8✔
1817
            wt.commit();
8✔
1818
        }
8✔
1819

4✔
1820
        rt->advance_read();
8✔
1821
        rt->verify();
8✔
1822
        // Leave `group` and the target accessors in a state which can be tested
4✔
1823
        // with the changes applied
4✔
1824
    };
8✔
1825

1✔
1826

1✔
1827
    // Break link by clearing list
1✔
1828
    perform_change([&](Table& origin) {
2✔
1829
        origin.get_object(1).get_linklist(col).clear();
2✔
1830
    });
2✔
1831
    CHECK(target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1832
    CHECK_EQUAL(target->size(), 1);
2✔
1833

1✔
1834
    // Break link by removal from list
1✔
1835
    perform_change([&](Table& origin) {
2✔
1836
        origin.get_object(1).get_linklist(col).remove(0);
2✔
1837
    });
2✔
1838
    CHECK(target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1839
    CHECK_EQUAL(target->size(), 1);
2✔
1840

1✔
1841
    // Break link by reassign
1✔
1842
    perform_change([&](Table& origin) {
2✔
1843
        origin.get_object(1).get_linklist(col).create_and_set_linked_object(0);
2✔
1844
    });
2✔
1845
    CHECK(target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1846
    CHECK_EQUAL(target->size(), 2);
2✔
1847

1✔
1848
    // Break link by clearing table
1✔
1849
    perform_change([](Table& origin) {
2✔
1850
        origin.clear();
2✔
1851
    });
2✔
1852
    CHECK(!target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1853
    CHECK_EQUAL(target->size(), 0);
2✔
1854
}
2✔
1855

1856

1857
TEST(LangBindHelper_AdvanceReadTransact_IntIndex)
1858
{
2✔
1859
    SHARED_GROUP_TEST_PATH(path);
2✔
1860

1✔
1861
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1862
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1863
    auto g = sg->start_read();
2✔
1864
    g->promote_to_write();
2✔
1865

1✔
1866
    TableRef target = g->add_table("target");
2✔
1867
    auto col = target->add_column(type_Int, "pk");
2✔
1868
    target->add_search_index(col);
2✔
1869

1✔
1870
    std::vector<ObjKey> obj_keys;
2✔
1871
    target->create_objects(REALM_MAX_BPNODE_SIZE + 1, obj_keys);
2✔
1872

1✔
1873
    g->commit_and_continue_as_read();
2✔
1874

1✔
1875
    // open a second copy that'll be advanced over the write
1✔
1876
    auto g_r = sg->start_read();
2✔
1877
    TableRef t_r = g_r->get_table("target");
2✔
1878

1✔
1879
    g->promote_to_write();
2✔
1880

1✔
1881
    // Ensure that the index has a different bptree layout so that failing to
1✔
1882
    // refresh it will do bad things
1✔
1883
    int i = 0;
2✔
1884
    for (auto it = target->begin(); it != target->end(); ++it)
2,004✔
1885
        it->set(col, i++);
2,002✔
1886

1✔
1887
    g->commit_and_continue_as_read();
2✔
1888

1✔
1889
    g_r->promote_to_write();
2✔
1890
    // Crashes if index has an invalid parent ref
1✔
1891
    t_r->clear();
2✔
1892
}
2✔
1893

1894
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_TableClear, testing_supports_spawn_process)
1895
{
2✔
1896
    SHARED_GROUP_TEST_PATH(path);
2✔
1897

1✔
1898
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1899
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1900
    ColKey col;
2✔
1901
    if (SpawnedProcess::is_parent()) {
2✔
1902
        WriteTransaction wt(sg);
2✔
1903
        TableRef table = wt.add_table("table");
2✔
1904
        col = table->add_column(type_Int, "col");
2✔
1905
        table->create_object();
2✔
1906
        wt.commit();
2✔
1907
    }
2✔
1908

1✔
1909
    auto reader = sg->start_read();
2✔
1910
    auto table = reader->get_table("table");
2✔
1911
    TableView tv = table->where().find_all();
2✔
1912
    auto obj = *table->begin();
2✔
1913
    CHECK(obj.is_valid());
2✔
1914

1✔
1915
    auto process = test_util::spawn_process(test_context.test_details.test_name, "external_clear");
2✔
1916
    if (process->is_child()) {
2✔
1917
        {
×
1918
            std::unique_ptr<Replication> hist_w(make_in_realm_history());
×
1919
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
1920
            WriteTransaction wt(sg_w);
×
1921
            wt.get_table("table")->clear();
×
1922
            wt.commit();
×
1923
        }
×
1924
        exit(0);
×
1925
    }
×
1926
    else if (process->is_parent()) {
2✔
1927
        process->wait_for_child_to_finish();
2✔
1928

1✔
1929
        reader->advance_read();
2✔
1930

1✔
1931
        CHECK(!obj.is_valid());
2✔
1932

1✔
1933
        CHECK_EQUAL(tv.size(), 1);
2✔
1934
        CHECK(!tv.is_in_sync());
2✔
1935
        // key is still there...
1✔
1936
        CHECK(tv.get_key(0));
2✔
1937
        // but no obj for that key...
1✔
1938
        CHECK_NOT(tv.get_object(0).is_valid());
2✔
1939

1✔
1940
        tv.sync_if_needed();
2✔
1941
        CHECK_EQUAL(tv.size(), 0);
2✔
1942
    }
2✔
1943
}
2✔
1944

1945
TEST(LangBindHelper_AdvanceReadTransact_UnorderedTableViewClear)
1946
{
2✔
1947
    SHARED_GROUP_TEST_PATH(path);
2✔
1948

1✔
1949
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1950
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1951
    ObjKey first_obj, last_obj;
2✔
1952
    ColKey col;
2✔
1953
    {
2✔
1954
        WriteTransaction wt(sg);
2✔
1955
        TableRef table = wt.add_table("table");
2✔
1956
        col = table->add_column(type_Int, "col");
2✔
1957
        first_obj = table->create_object().set_all(0).get_key();
2✔
1958
        table->create_object().set_all(1);
2✔
1959
        last_obj = table->create_object().set_all(2).get_key();
2✔
1960
        wt.commit();
2✔
1961
    }
2✔
1962

1✔
1963
    auto reader = sg->start_read();
2✔
1964
    auto table = reader->get_table("table");
2✔
1965
    auto obj = table->get_object(last_obj);
2✔
1966
    CHECK_EQUAL(obj.get<int64_t>(col), 2);
2✔
1967

1✔
1968
    {
2✔
1969
        // Remove the first row via unordered removal, resulting in the '2' row
1✔
1970
        // moving to index 0 (with ordered removal it would instead move to index 1)
1✔
1971
        WriteTransaction wt(sg);
2✔
1972
        wt.get_table("table")->where().equal(col, 0).find_all().clear();
2✔
1973
        wt.commit();
2✔
1974
    }
2✔
1975

1✔
1976
    reader->advance_read();
2✔
1977

1✔
1978
    CHECK(obj.is_valid());
2✔
1979
    CHECK_EQUAL(obj.get<int64_t>(col), 2);
2✔
1980
}
2✔
1981

1982
namespace {
1983
// A base class for transaction log parsers so that tests which want to test
1984
// just a single part of the transaction log handling don't have to implement
1985
// the entire interface
1986
class NoOpTransactionLogParser {
1987
public:
1988
    NoOpTransactionLogParser(TestContext& context)
1989
        : test_context(context)
1990
    {
22✔
1991
    }
22✔
1992

1993
    TableKey get_current_table() const
1994
    {
8✔
1995
        return m_current_table;
8✔
1996
    }
8✔
1997

1998
    std::pair<ColKey, ObjKey> get_current_linkview() const
1999
    {
×
2000
        return {m_current_linkview_col, m_current_linkview_row};
×
2001
    }
×
2002

2003
protected:
2004
    TestContext& test_context;
2005

2006
private:
2007
    TableKey m_current_table;
2008
    ColKey m_current_linkview_col;
2009
    ObjKey m_current_linkview_row;
2010

2011
public:
2012
    void parse_complete() {}
12✔
2013

2014
    bool select_table(TableKey t)
2015
    {
30✔
2016
        m_current_table = t;
30✔
2017
        return true;
30✔
2018
    }
30✔
2019

2020
    bool select_collection(ColKey col_key, ObjKey obj_key)
2021
    {
4✔
2022
        m_current_linkview_col = col_key;
4✔
2023
        m_current_linkview_row = obj_key;
4✔
2024
        return true;
4✔
2025
    }
4✔
2026

2027
    // Default no-op implementations of all of the mutation instructions
2028
    bool insert_group_level_table(TableKey)
2029
    {
×
2030
        return false;
×
2031
    }
×
2032
    bool erase_class(TableKey)
2033
    {
×
2034
        return false;
×
2035
    }
×
2036
    bool rename_class(TableKey)
2037
    {
×
2038
        return false;
×
2039
    }
×
2040
    bool insert_column(ColKey)
2041
    {
×
2042
        return false;
×
2043
    }
×
2044
    bool erase_column(ColKey)
2045
    {
×
2046
        return false;
×
2047
    }
×
2048
    bool rename_column(ColKey)
2049
    {
×
2050
        return false;
×
2051
    }
×
2052
    bool set_link_type(ColKey)
2053
    {
×
2054
        return false;
×
2055
    }
×
2056
    bool create_object(ObjKey)
2057
    {
×
2058
        return false;
×
2059
    }
×
2060
    bool remove_object(ObjKey)
2061
    {
×
2062
        return false;
×
2063
    }
×
2064
    bool collection_set(size_t)
2065
    {
×
2066
        return false;
×
2067
    }
×
2068
    bool collection_clear(size_t)
2069
    {
×
2070
        return false;
×
2071
    }
×
2072
    bool collection_erase(size_t)
2073
    {
×
2074
        return false;
×
2075
    }
×
2076
    bool collection_insert(size_t)
2077
    {
×
2078
        return false;
×
2079
    }
×
2080
    bool collection_move(size_t, size_t)
2081
    {
×
2082
        return false;
×
2083
    }
×
2084
    bool modify_object(ColKey, ObjKey)
2085
    {
×
2086
        return false;
×
2087
    }
×
2088
    bool typed_link_change(ColKey, TableKey)
2089
    {
×
2090
        return true;
×
2091
    }
×
2092
};
2093

2094
struct AdvanceReadTransact {
2095
    template <typename Func>
2096
    static void call(TransactionRef tr, Func* func)
2097
    {
10✔
2098
        tr->advance_read(func);
10✔
2099
    }
10✔
2100
};
2101

2102
struct PromoteThenRollback {
2103
    template <typename Func>
2104
    static void call(TransactionRef tr, Func* func)
2105
    {
10✔
2106
        tr->promote_to_write(func);
10✔
2107
        tr->rollback_and_continue_as_read();
10✔
2108
    }
10✔
2109
};
2110

2111
} // unnamed namespace
2112

2113
TEST_TYPES(LangBindHelper_AdvanceReadTransact_TransactLog, AdvanceReadTransact, PromoteThenRollback)
2114
{
4✔
2115
    SHARED_GROUP_TEST_PATH(path);
4✔
2116
    std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
2117
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
2118
    ColKey c0, c1;
4✔
2119
    {
4✔
2120
        WriteTransaction wt(sg);
4✔
2121
        c0 = wt.add_table("table 1")->add_column(type_Int, "int");
4✔
2122
        c1 = wt.add_table("table 2")->add_column(type_Int, "int");
4✔
2123
        wt.commit();
4✔
2124
    }
4✔
2125

2✔
2126
    auto tr = sg->start_read();
4✔
2127

2✔
2128
    {
4✔
2129
        // With no changes, the handler should not be called at all
2✔
2130
        struct : NoOpTransactionLogParser {
4✔
2131
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2132
            void parse_complete()
4✔
2133
            {
2✔
2134
                CHECK(false);
×
2135
            }
×
2136
        } parser(test_context);
4✔
2137
        TEST_TYPE::call(tr, &parser);
4✔
2138
    }
4✔
2139

2✔
2140
    {
4✔
2141
        // With an empty change, parse_complete() and nothing else should be called
2✔
2142
        auto wt = sg->start_write();
4✔
2143
        wt->commit();
4✔
2144

2✔
2145
        struct foo : NoOpTransactionLogParser {
4✔
2146
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2147

2✔
2148
            bool called = false;
4✔
2149
            void parse_complete()
4✔
2150
            {
4✔
2151
                called = true;
4✔
2152
            }
4✔
2153
        } parser(test_context);
4✔
2154
        TEST_TYPE::call(tr, &parser);
4✔
2155
        CHECK(parser.called);
4✔
2156
    }
4✔
2157
    ObjKey o0, o1;
4✔
2158
    {
4✔
2159
        // Make a simple modification and verify that the appropriate handler is called
2✔
2160
        struct foo : NoOpTransactionLogParser {
4✔
2161
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2162

2✔
2163
            size_t expected_table = 0;
4✔
2164
            TableKey t1;
4✔
2165
            TableKey t2;
4✔
2166

2✔
2167
            bool create_object(ObjKey)
4✔
2168
            {
8✔
2169
                CHECK_EQUAL(expected_table ? t2 : t1, get_current_table());
8✔
2170
                ++expected_table;
8✔
2171

4✔
2172
                return true;
8✔
2173
            }
8✔
2174
        } parser(test_context);
4✔
2175

2✔
2176
        WriteTransaction wt(sg);
4✔
2177
        parser.t1 = wt.get_table("table 1")->get_key();
4✔
2178
        parser.t2 = wt.get_table("table 2")->get_key();
4✔
2179
        o0 = wt.get_table("table 1")->create_object().get_key();
4✔
2180
        o1 = wt.get_table("table 2")->create_object().get_key();
4✔
2181
        wt.commit();
4✔
2182

2✔
2183
        TEST_TYPE::call(tr, &parser);
4✔
2184
        CHECK_EQUAL(2, parser.expected_table);
4✔
2185
    }
4✔
2186
    ColKey c2, c3;
4✔
2187
    ObjKey okey;
4✔
2188
    {
4✔
2189
        // Add a table with some links
2✔
2190
        WriteTransaction wt(sg);
4✔
2191
        TableRef table = wt.add_table("link origin");
4✔
2192
        c2 = table->add_column(*wt.get_table("table 1"), "link");
4✔
2193
        c3 = table->add_column_list(*wt.get_table("table 2"), "linklist");
4✔
2194
        Obj o = table->create_object();
4✔
2195
        o.set(c2, o.get_key());
4✔
2196
        o.get_linklist(c3).add(o.get_key());
4✔
2197
        okey = o.get_key();
4✔
2198
        wt.commit();
4✔
2199

2✔
2200
        tr->advance_read();
4✔
2201
    }
4✔
2202
    {
4✔
2203
        // Verify that deleting the targets of the links logs link nullifications
2✔
2204
        WriteTransaction wt(sg);
4✔
2205
        wt.get_table("table 1")->remove_object(o0);
4✔
2206
        wt.get_table("table 2")->remove_object(o1);
4✔
2207
        wt.commit();
4✔
2208

2✔
2209
        struct : NoOpTransactionLogParser {
4✔
2210
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2211

2✔
2212
            bool remove_object(ObjKey o)
4✔
2213
            {
8✔
2214
                CHECK(o == o1 || o == o0);
8!
2215
                return true;
8✔
2216
            }
8✔
2217
            bool select_collection(ColKey col, ObjKey o)
4✔
2218
            {
4✔
2219
                CHECK(col == link_list_col);
4✔
2220
                CHECK(o == okey);
4✔
2221
                return true;
4✔
2222
            }
4✔
2223
            bool collection_erase(size_t ndx)
4✔
2224
            {
4✔
2225
                CHECK(ndx == 0);
4✔
2226
                return true;
4✔
2227
            }
4✔
2228

2✔
2229
            bool modify_object(ColKey col, ObjKey obj)
4✔
2230
            {
4✔
2231
                CHECK(col == link_col && obj == okey);
4✔
2232
                return true;
4✔
2233
            }
4✔
2234
            ObjKey o0, o1, okey;
4✔
2235
            ColKey link_col, link_list_col;
4✔
2236
        } parser(test_context);
4✔
2237
        parser.o1 = o1;
4✔
2238
        parser.o0 = o0;
4✔
2239
        parser.okey = okey;
4✔
2240
        parser.link_col = c2;
4✔
2241
        parser.link_list_col = c3;
4✔
2242
        TEST_TYPE::call(tr, &parser);
4✔
2243
    }
4✔
2244
    {
4✔
2245
        // Verify that clear() logs the correct rows
2✔
2246
        WriteTransaction wt(sg);
4✔
2247
        std::vector<ObjKey> keys;
4✔
2248
        wt.get_table("table 2")->create_objects(10, keys);
4✔
2249

2✔
2250
        auto lv = wt.get_table("link origin")->begin()->get_linklist(c3);
4✔
2251
        lv.add(keys[1]);
4✔
2252
        lv.add(keys[3]);
4✔
2253
        lv.add(keys[5]);
4✔
2254

2✔
2255
        wt.commit();
4✔
2256
        tr->advance_read();
4✔
2257
    }
4✔
2258
    {
4✔
2259
        WriteTransaction wt(sg);
4✔
2260
        wt.get_table("link origin")->begin()->get_linklist(c3).clear();
4✔
2261
        wt.commit();
4✔
2262
        struct : NoOpTransactionLogParser {
4✔
2263
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2264

2✔
2265
            bool collection_clear(size_t old_size) const
4✔
2266
            {
4✔
2267
                CHECK_EQUAL(3, old_size);
4✔
2268
                return true;
4✔
2269
            }
4✔
2270
        } parser(test_context);
4✔
2271
        TEST_TYPE::call(tr, &parser);
4✔
2272
    }
4✔
2273
}
4✔
2274

2275

2276
TEST(LangBindHelper_AdvanceReadTransact_ErrorInObserver)
2277
{
2✔
2278
    SHARED_GROUP_TEST_PATH(path);
2✔
2279
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2280
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2281
    ColKey col;
2✔
2282
    Obj obj;
2✔
2283
    // Add some initial data and then begin a read transaction at that version
1✔
2284
    auto wt1 = sg->start_write();
2✔
2285
    TableRef table = wt1->add_table("Table");
2✔
2286
    col = table->add_column(type_Int, "int");
2✔
2287
    auto obj2 = table->create_object().set_all(10);
2✔
2288
    wt1->commit_and_continue_as_read();
2✔
2289

1✔
2290
    auto g = sg->start_read();     // must follow commit, to see table just created
2✔
2291
    obj = g->import_copy_of(obj2); // cannot be imported if table does not exist
2✔
2292
    wt1->end_read();               // wt1 must live long enough to support import_copy_of of obj2
2✔
2293
    // Modify the data with a different SG so that we can determine which version
1✔
2294
    // the read transaction is using
1✔
2295
    {
2✔
2296
        auto wt = sg->start_write();
2✔
2297
        Obj o2 = wt->import_copy_of(obj);
2✔
2298
        o2.set<int64_t>(col, 20);
2✔
2299
        wt->commit();
2✔
2300
    }
2✔
2301

1✔
2302
    struct ObserverError {
2✔
2303
    };
2✔
2304
    try {
2✔
2305
        struct : NoOpTransactionLogParser {
2✔
2306
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
2✔
2307

1✔
2308
            bool modify_object(ColKey, ObjKey) const
2✔
2309
            {
2✔
2310
                throw ObserverError();
2✔
2311
            }
2✔
2312
        } parser(test_context);
2✔
2313
        g->advance_read(&parser);
2✔
2314
        CHECK(false); // Should not be reached
2✔
2315
    }
2✔
2316
    catch (ObserverError) {
2✔
2317
    }
2✔
2318

1✔
2319
    // Should still see data from old version
1✔
2320
    auto o = g->import_copy_of(obj);
2✔
2321
    CHECK_EQUAL(10, o.get<int64_t>(col));
2✔
2322

1✔
2323
    // Should be able to advance to the new version still
1✔
2324
    g->advance_read();
2✔
2325

1✔
2326
    // And see that version's data
1✔
2327
    CHECK_EQUAL(20, o.get<int64_t>(col));
2✔
2328
}
2✔
2329

2330

2331
TEST(LangBindHelper_ImplicitTransactions)
2332
{
2✔
2333
    SHARED_GROUP_TEST_PATH(path);
2✔
2334
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2335
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2336
    ObjKey o;
2✔
2337
    ColKey col;
2✔
2338
    {
2✔
2339
        WriteTransaction wt(sg);
2✔
2340
        auto table = wt.add_table("table");
2✔
2341
        col = table->add_column(type_Int, "first");
2✔
2342
        table->add_column(type_Int, "second");
2✔
2343
        table->add_column(type_Bool, "third");
2✔
2344
        table->add_column(type_String, "fourth");
2✔
2345
        o = table->create_object().get_key();
2✔
2346
        wt.commit();
2✔
2347
    }
2✔
2348
    auto g = sg->start_read();
2✔
2349
    auto table = g->get_table("table");
2✔
2350
    for (int i = 0; i < 100; i++) {
202✔
2351
        {
200✔
2352
            // change table in other context
100✔
2353
            WriteTransaction wt(sg);
200✔
2354
            wt.get_table("table")->get_object(o).add_int(col, 100);
200✔
2355
            wt.commit();
200✔
2356
        }
200✔
2357
        // verify we can't see the update
100✔
2358
        CHECK_EQUAL(i, table->get_object(o).get<int64_t>(col));
200✔
2359
        g->advance_read();
200✔
2360
        // now we CAN see it, and through the same accessor
100✔
2361
        CHECK(table);
200✔
2362
        CHECK_EQUAL(i + 100, table->get_object(o).get<int64_t>(col));
200✔
2363
        {
200✔
2364
            // change table in other context
100✔
2365
            WriteTransaction wt(sg);
200✔
2366
            wt.get_table("table")->get_object(o).add_int(col, 10000);
200✔
2367
            wt.commit();
200✔
2368
        }
200✔
2369
        // can't see it:
100✔
2370
        CHECK_EQUAL(i + 100, table->get_object(o).get<int64_t>(col));
200✔
2371
        g->promote_to_write();
200✔
2372
        // CAN see it:
100✔
2373
        CHECK(table);
200✔
2374
        CHECK_EQUAL(i + 10100, table->get_object(o).get<int64_t>(col));
200✔
2375
        table->get_object(o).add_int(col, -10100);
200✔
2376
        table->get_object(o).add_int(col, 1);
200✔
2377
        g->commit_and_continue_as_read();
200✔
2378
        CHECK(table);
200✔
2379
        CHECK_EQUAL(i + 1, table->get_object(o).get<int64_t>(col));
200✔
2380
    }
200✔
2381
    g->end_read();
2✔
2382
}
2✔
2383

2384

2385
TEST(LangBindHelper_RollbackAndContinueAsRead)
2386
{
2✔
2387
    SHARED_GROUP_TEST_PATH(path);
2✔
2388
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2389
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2390
    {
2✔
2391
        ObjKey key;
2✔
2392
        ColKey col;
2✔
2393
        auto group = sg->start_read();
2✔
2394
        {
2✔
2395
            group->promote_to_write();
2✔
2396
            TableRef origin = group->get_or_add_table("origin");
2✔
2397
            col = origin->add_column(type_Int, "");
2✔
2398
            key = origin->create_object().set_all(42).get_key();
2✔
2399
            group->commit_and_continue_as_read();
2✔
2400
        }
2✔
2401
        group->verify();
2✔
2402
        {
2✔
2403
            // rollback of group level table insertion
1✔
2404
            group->promote_to_write();
2✔
2405
            group->get_or_add_table("nullermand");
2✔
2406
            TableRef o2 = group->get_table("nullermand");
2✔
2407
            REALM_ASSERT(o2);
2✔
2408
            group->rollback_and_continue_as_read();
2✔
2409
            TableRef o3 = group->get_table("nullermand");
2✔
2410
            REALM_ASSERT(!o3);
2✔
2411
            REALM_ASSERT(!o2);
2✔
2412
        }
2✔
2413

1✔
2414
        TableRef origin = group->get_table("origin");
2✔
2415
        Obj row = origin->get_object(key);
2✔
2416
        CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2417

1✔
2418
        {
2✔
2419
            group->promote_to_write();
2✔
2420
            auto row2 = origin->create_object().set_all(5746);
2✔
2421
            CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2422
            CHECK_EQUAL(5746, row2.get<int64_t>(col));
2✔
2423
            CHECK_EQUAL(2, origin->size());
2✔
2424
            group->verify();
2✔
2425
            group->rollback_and_continue_as_read();
2✔
2426
        }
2✔
2427
        CHECK_EQUAL(1, origin->size());
2✔
2428
        group->verify();
2✔
2429
        CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2430
        Obj row2;
2✔
2431
        {
2✔
2432
            group->promote_to_write();
2✔
2433
            row2 = origin->create_object().set_all(42);
2✔
2434
            group->commit_and_continue_as_read();
2✔
2435
        }
2✔
2436
        CHECK_EQUAL(2, origin->size());
2✔
2437
        group->verify();
2✔
2438
        CHECK_EQUAL(42, row2.get<int64_t>(col));
2✔
2439
        group->end_read();
2✔
2440
    }
2✔
2441
}
2✔
2442

2443

2444
TEST(LangBindHelper_RollbackAndContinueAsReadGroupLevelTableRemoval)
2445
{
2✔
2446
    SHARED_GROUP_TEST_PATH(path);
2✔
2447
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2448
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2449
    auto reader = sg->start_read();
2✔
2450
    {
2✔
2451
        reader->promote_to_write();
2✔
2452
        reader->get_or_add_table("a_table");
2✔
2453
        reader->commit_and_continue_as_read();
2✔
2454
    }
2✔
2455
    reader->verify();
2✔
2456
    {
2✔
2457
        // rollback of group level table delete
1✔
2458
        reader->promote_to_write();
2✔
2459
        TableRef o2 = reader->get_table("a_table");
2✔
2460
        REALM_ASSERT(o2);
2✔
2461
        reader->remove_table("a_table");
2✔
2462
        TableRef o3 = reader->get_table("a_table");
2✔
2463
        REALM_ASSERT(!o3);
2✔
2464
        reader->rollback_and_continue_as_read();
2✔
2465
        TableRef o4 = reader->get_table("a_table");
2✔
2466
        REALM_ASSERT(o4);
2✔
2467
    }
2✔
2468
    reader->verify();
2✔
2469
}
2✔
2470

2471
TEST(LangBindHelper_RollbackCircularReferenceRemoval)
2472
{
2✔
2473
    SHARED_GROUP_TEST_PATH(path);
2✔
2474
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2475
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2476
    ColKey ca, cb;
2✔
2477
    auto group = sg->start_read();
2✔
2478
    {
2✔
2479
        group->promote_to_write();
2✔
2480
        TableRef alpha = group->get_or_add_table("alpha");
2✔
2481
        TableRef beta = group->get_or_add_table("beta");
2✔
2482
        ca = alpha->add_column(*beta, "beta-1");
2✔
2483
        cb = beta->add_column(*alpha, "alpha-1");
2✔
2484
        group->commit_and_continue_as_read();
2✔
2485
    }
2✔
2486
    group->verify();
2✔
2487
    {
2✔
2488
        group->promote_to_write();
2✔
2489
        CHECK_EQUAL(2, group->size());
2✔
2490
        TableRef alpha = group->get_table("alpha");
2✔
2491
        TableRef beta = group->get_table("beta");
2✔
2492

1✔
2493
        CHECK_THROW(group->remove_table("alpha"), CrossTableLinkTarget);
2✔
2494
        beta->remove_column(cb);
2✔
2495
        alpha->remove_column(ca);
2✔
2496
        group->remove_table("beta");
2✔
2497
        CHECK_NOT(group->has_table("beta"));
2✔
2498

1✔
2499
        // Version 1: This crashes
1✔
2500
        group->rollback_and_continue_as_read();
2✔
2501
        CHECK_EQUAL(2, group->size());
2✔
2502

1✔
2503
        //        // Version 2: This works
1✔
2504
        //        LangBindHelper::commit_and_continue_as_read(sg);
1✔
2505
        //        CHECK_EQUAL(1, group->size());
1✔
2506
    }
2✔
2507
    group->verify();
2✔
2508
}
2✔
2509

2510

2511
TEST(LangBindHelper_RollbackAndContinueAsReadColumnAdd)
2512
{
2✔
2513
    SHARED_GROUP_TEST_PATH(path);
2✔
2514
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2515
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2516
    auto group = sg->start_read();
2✔
2517
    TableRef t;
2✔
2518
    {
2✔
2519
        group->promote_to_write();
2✔
2520
        t = group->get_or_add_table("a_table");
2✔
2521
        t->add_column(type_Int, "lorelei");
2✔
2522
        t->create_object().set_all(43);
2✔
2523
        CHECK_EQUAL(1, t->get_column_count());
2✔
2524
        group->commit_and_continue_as_read();
2✔
2525
    }
2✔
2526
    group->verify();
2✔
2527
    {
2✔
2528
        // add a column and regret it again
1✔
2529
        group->promote_to_write();
2✔
2530
        auto col = t->add_column(type_Int, "riget");
2✔
2531
        t->begin()->set(col, 44);
2✔
2532
        CHECK_EQUAL(2, t->get_column_count());
2✔
2533
        group->verify();
2✔
2534
        group->rollback_and_continue_as_read();
2✔
2535
        group->verify();
2✔
2536
        CHECK_EQUAL(1, t->get_column_count());
2✔
2537
    }
2✔
2538
    group->verify();
2✔
2539
}
2✔
2540

2541

2542
// This issue was uncovered while looking into the RollbackCircularReferenceRemoval issue
2543
TEST(LangBindHelper_TableLinkingRemovalIssue)
2544
{
2✔
2545
    SHARED_GROUP_TEST_PATH(path);
2✔
2546
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2547
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2548
    auto group = sg->start_read();
2✔
2549
    {
2✔
2550
        group->promote_to_write();
2✔
2551
        TableRef t1 = group->get_or_add_table("t1");
2✔
2552
        TableRef t2 = group->get_or_add_table("t2");
2✔
2553
        TableRef t3 = group->get_or_add_table("t3");
2✔
2554
        TableRef t4 = group->get_or_add_table("t4");
2✔
2555
        t1->add_column(*t2, "l12");
2✔
2556
        t2->add_column(*t3, "l23");
2✔
2557
        t3->add_column(*t4, "l34");
2✔
2558
        group->commit_and_continue_as_read();
2✔
2559
    }
2✔
2560
    group->verify();
2✔
2561
    {
2✔
2562
        group->promote_to_write();
2✔
2563
        CHECK_EQUAL(4, group->size());
2✔
2564

1✔
2565
        group->remove_table("t1");
2✔
2566
        group->remove_table("t2");
2✔
2567
        group->remove_table("t3"); // CRASHES HERE
2✔
2568
        group->remove_table("t4");
2✔
2569

1✔
2570
        group->rollback_and_continue_as_read();
2✔
2571
        CHECK_EQUAL(4, group->size());
2✔
2572
    }
2✔
2573
    group->verify();
2✔
2574
}
2✔
2575

2576

2577
// This issue was uncovered while looking into the RollbackCircularReferenceRemoval issue
2578
TEST(LangBindHelper_RollbackTableRemove)
2579
{
2✔
2580
    SHARED_GROUP_TEST_PATH(path);
2✔
2581
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2582
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2583
    auto group = sg->start_read();
2✔
2584
    {
2✔
2585
        group->promote_to_write();
2✔
2586
        TableRef alpha = group->get_or_add_table("alpha");
2✔
2587
        TableRef beta = group->get_or_add_table("beta");
2✔
2588
        beta->add_column(*alpha, "alpha-1");
2✔
2589
        group->commit_and_continue_as_read();
2✔
2590
    }
2✔
2591
    group->verify();
2✔
2592
    {
2✔
2593
        group->promote_to_write();
2✔
2594
        CHECK_EQUAL(2, group->size());
2✔
2595
        TableRef alpha = group->get_table("alpha");
2✔
2596
        TableRef beta = group->get_table("beta");
2✔
2597
        CHECK(alpha);
2✔
2598
        CHECK(beta);
2✔
2599
        group->remove_table("beta");
2✔
2600
        CHECK_NOT(group->has_table("beta"));
2✔
2601
        group->rollback_and_continue_as_read();
2✔
2602
        CHECK_EQUAL(2, group->size());
2✔
2603
    }
2✔
2604
    group->verify();
2✔
2605
}
2✔
2606

2607
TEST(LangBindHelper_RollbackTableRemove2)
2608
{
2✔
2609
    SHARED_GROUP_TEST_PATH(path);
2✔
2610
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2611
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2612
    auto group = sg->start_read();
2✔
2613
    {
2✔
2614
        group->promote_to_write();
2✔
2615
        TableRef a = group->get_or_add_table("a");
2✔
2616
        TableRef b = group->get_or_add_table("b");
2✔
2617
        TableRef c = group->get_or_add_table("c");
2✔
2618
        TableRef d = group->get_or_add_table("d");
2✔
2619
        c->add_column(*a, "a");
2✔
2620
        d->add_column(*b, "b");
2✔
2621
        group->commit_and_continue_as_read();
2✔
2622
    }
2✔
2623
    group->verify();
2✔
2624
    {
2✔
2625
        group->promote_to_write();
2✔
2626
        CHECK_EQUAL(4, group->size());
2✔
2627
        group->remove_table("c");
2✔
2628
        CHECK_NOT(group->has_table("c"));
2✔
2629
        group->verify();
2✔
2630
        group->rollback_and_continue_as_read();
2✔
2631
        CHECK_EQUAL(4, group->size());
2✔
2632
    }
2✔
2633
    group->verify();
2✔
2634
}
2✔
2635

2636
TEST(LangBindHelper_ContinuousTransactions_RollbackTableRemoval)
2637
{
2✔
2638
    // Test that it is possible to modify a table, then remove it from the
1✔
2639
    // group, and then rollback the transaction.
1✔
2640

1✔
2641
    // This triggered a bug in the instruction reverser which would incorrectly
1✔
2642
    // associate the table removal instruction with the table selection
1✔
2643
    // instruction induced by the modification, causing the latter to occur in
1✔
2644
    // the reverse log at a point where the selected table does not yet
1✔
2645
    // exist. The filler table is there to avoid an early-out in
1✔
2646
    // Group::TransactAdvancer::select_table() due to a misinterpretation of the
1✔
2647
    // reason for the missing table accessor entry.
1✔
2648

1✔
2649
    SHARED_GROUP_TEST_PATH(path);
2✔
2650
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2651
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2652
    auto group = sg->start_read();
2✔
2653
    group->promote_to_write();
2✔
2654
    group->get_or_add_table("filler");
2✔
2655
    TableRef table = group->get_or_add_table("table");
2✔
2656
    auto col = table->add_column(type_Int, "i");
2✔
2657
    Obj o = table->create_object();
2✔
2658
    group->commit_and_continue_as_read();
2✔
2659
    group->promote_to_write();
2✔
2660
    o.set<int>(col, 0);
2✔
2661
    group->remove_table("table");
2✔
2662
    group->rollback_and_continue_as_read();
2✔
2663
}
2✔
2664

2665
TEST(LangBindHelper_RollbackAndContinueAsReadLinkColumnRemove)
2666
{
2✔
2667
    SHARED_GROUP_TEST_PATH(path);
2✔
2668
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2669
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2670
    auto group = sg->start_read();
2✔
2671
    TableRef t, t2;
2✔
2672
    ColKey col;
2✔
2673
    {
2✔
2674
        // add a column
1✔
2675
        group->promote_to_write();
2✔
2676
        t = group->get_or_add_table("a_table");
2✔
2677
        t2 = group->get_or_add_table("b_table");
2✔
2678
        col = t->add_column(*t2, "bruno");
2✔
2679
        CHECK_EQUAL(1, t->get_column_count());
2✔
2680
        group->commit_and_continue_as_read();
2✔
2681
    }
2✔
2682
    group->verify();
2✔
2683
    {
2✔
2684
        // ... but then regret it
1✔
2685
        group->promote_to_write();
2✔
2686
        t->remove_column(col);
2✔
2687
        CHECK_EQUAL(0, t->get_column_count());
2✔
2688
        group->rollback_and_continue_as_read();
2✔
2689
    }
2✔
2690
}
2✔
2691

2692

2693
TEST(LangBindHelper_RollbackAndContinueAsReadColumnRemove)
2694
{
2✔
2695
    SHARED_GROUP_TEST_PATH(path);
2✔
2696
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2697
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2698
    auto group = sg->start_read();
2✔
2699
    TableRef t;
2✔
2700
    ColKey col;
2✔
2701
    {
2✔
2702
        group->promote_to_write();
2✔
2703
        t = group->get_or_add_table("a_table");
2✔
2704
        col = t->add_column(type_Int, "lorelei");
2✔
2705
        t->add_column(type_Int, "riget");
2✔
2706
        t->create_object().set_all(43, 44);
2✔
2707
        CHECK_EQUAL(2, t->get_column_count());
2✔
2708
        group->commit_and_continue_as_read();
2✔
2709
    }
2✔
2710
    group->verify();
2✔
2711
    {
2✔
2712
        // remove a column but regret it
1✔
2713
        group->promote_to_write();
2✔
2714
        CHECK_EQUAL(2, t->get_column_count());
2✔
2715
        t->remove_column(col);
2✔
2716
        group->verify();
2✔
2717
        group->rollback_and_continue_as_read();
2✔
2718
        group->verify();
2✔
2719
        CHECK_EQUAL(2, t->get_column_count());
2✔
2720
    }
2✔
2721
    group->verify();
2✔
2722
}
2✔
2723

2724

2725
TEST(LangBindHelper_RollbackAndContinueAsReadLinkList)
2726
{
2✔
2727
    SHARED_GROUP_TEST_PATH(path);
2✔
2728
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2729
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2730
    auto group = sg->start_read();
2✔
2731
    group->promote_to_write();
2✔
2732
    TableRef origin = group->add_table("origin");
2✔
2733
    TableRef target = group->add_table("target");
2✔
2734
    auto col0 = origin->add_column_list(*target, "");
2✔
2735
    target->add_column(type_Int, "");
2✔
2736
    auto o0 = origin->create_object();
2✔
2737
    auto t0 = target->create_object();
2✔
2738
    auto t1 = target->create_object();
2✔
2739
    auto t2 = target->create_object();
2✔
2740

1✔
2741
    auto link_list = o0.get_linklist(col0);
2✔
2742
    link_list.add(t0.get_key());
2✔
2743
    group->commit_and_continue_as_read();
2✔
2744
    CHECK_EQUAL(1, link_list.size());
2✔
2745
    group->verify();
2✔
2746
    // now change a link in link list and roll back the change
1✔
2747
    group->promote_to_write();
2✔
2748
    link_list.add(t1.get_key());
2✔
2749
    link_list.add(t2.get_key());
2✔
2750
    CHECK_EQUAL(3, link_list.size());
2✔
2751
    group->rollback_and_continue_as_read();
2✔
2752
    CHECK_EQUAL(1, link_list.size());
2✔
2753
    group->promote_to_write();
2✔
2754
    link_list.remove(0);
2✔
2755
    CHECK_EQUAL(0, link_list.size());
2✔
2756
    group->rollback_and_continue_as_read();
2✔
2757
    CHECK_EQUAL(1, link_list.size());
2✔
2758
}
2✔
2759

2760

2761
TEST(LangBindHelper_RollbackAndContinueAsRead_Links)
2762
{
2✔
2763
    SHARED_GROUP_TEST_PATH(path);
2✔
2764
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2765
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2766
    auto group = sg->start_read();
2✔
2767
    group->promote_to_write();
2✔
2768
    TableRef origin = group->add_table("origin");
2✔
2769
    TableRef target = group->add_table("target");
2✔
2770
    auto col0 = origin->add_column(*target, "");
2✔
2771
    target->add_column(type_Int, "");
2✔
2772
    auto o0 = origin->create_object();
2✔
2773
    target->create_object();
2✔
2774
    auto t1 = target->create_object();
2✔
2775
    auto t2 = target->create_object();
2✔
2776

1✔
2777
    o0.set(col0, t2.get_key());
2✔
2778
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2779
    group->commit_and_continue_as_read();
2✔
2780

1✔
2781
    // verify that we can revert a link change:
1✔
2782
    group->promote_to_write();
2✔
2783
    o0.set(col0, t1.get_key());
2✔
2784
    CHECK_EQUAL(t1.get_key(), o0.get<ObjKey>(col0));
2✔
2785
    group->rollback_and_continue_as_read();
2✔
2786
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2787
    // verify that we can revert addition of a row in target table
1✔
2788
    group->promote_to_write();
2✔
2789
    target->create_object();
2✔
2790
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2791
    group->rollback_and_continue_as_read();
2✔
2792
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2793
}
2✔
2794

2795

2796
TEST(LangBindHelper_RollbackAndContinueAsRead_LinkLists)
2797
{
2✔
2798
    SHARED_GROUP_TEST_PATH(path);
2✔
2799
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2800
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2801
    auto group = sg->start_read();
2✔
2802
    group->promote_to_write();
2✔
2803
    TableRef origin = group->add_table("origin");
2✔
2804
    TableRef target = group->add_table("target");
2✔
2805
    auto col0 = origin->add_column_list(*target, "");
2✔
2806
    target->add_column(type_Int, "");
2✔
2807
    auto o0 = origin->create_object();
2✔
2808
    auto t0 = target->create_object();
2✔
2809
    auto t1 = target->create_object();
2✔
2810
    auto t2 = target->create_object();
2✔
2811

1✔
2812
    auto link_list = o0.get_linklist(col0);
2✔
2813
    link_list.add(t0.get_key());
2✔
2814
    link_list.add(t1.get_key());
2✔
2815
    link_list.add(t2.get_key());
2✔
2816
    link_list.add(t0.get_key());
2✔
2817
    link_list.add(t2.get_key());
2✔
2818
    group->commit_and_continue_as_read();
2✔
2819
    // verify that we can reverse a LinkView::move()
1✔
2820
    CHECK_EQUAL(5, link_list.size());
2✔
2821
    CHECK_EQUAL(t0.get_key(), link_list.get(0));
2✔
2822
    CHECK_EQUAL(t1.get_key(), link_list.get(1));
2✔
2823
    CHECK_EQUAL(t2.get_key(), link_list.get(2));
2✔
2824
    CHECK_EQUAL(t0.get_key(), link_list.get(3));
2✔
2825
    CHECK_EQUAL(t2.get_key(), link_list.get(4));
2✔
2826
    group->promote_to_write();
2✔
2827
    link_list.move(1, 3);
2✔
2828
    CHECK_EQUAL(5, link_list.size());
2✔
2829
    CHECK_EQUAL(t0.get_key(), link_list.get(0));
2✔
2830
    CHECK_EQUAL(t2.get_key(), link_list.get(1));
2✔
2831
    CHECK_EQUAL(t0.get_key(), link_list.get(2));
2✔
2832
    CHECK_EQUAL(t1.get_key(), link_list.get(3));
2✔
2833
    CHECK_EQUAL(t2.get_key(), link_list.get(4));
2✔
2834
    group->rollback_and_continue_as_read();
2✔
2835
    CHECK_EQUAL(5, link_list.size());
2✔
2836
    CHECK_EQUAL(t0.get_key(), link_list.get(0));
2✔
2837
    CHECK_EQUAL(t1.get_key(), link_list.get(1));
2✔
2838
    CHECK_EQUAL(t2.get_key(), link_list.get(2));
2✔
2839
    CHECK_EQUAL(t0.get_key(), link_list.get(3));
2✔
2840
    CHECK_EQUAL(t2.get_key(), link_list.get(4));
2✔
2841
}
2✔
2842

2843

2844
TEST(LangBindHelper_RollbackAndContinueAsRead_TableClear)
2845
{
2✔
2846
    SHARED_GROUP_TEST_PATH(path);
2✔
2847
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2848
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2849
    auto group = sg->start_read();
2✔
2850

1✔
2851
    group->promote_to_write();
2✔
2852
    TableRef origin = group->add_table("origin");
2✔
2853
    TableRef target = group->add_table("target");
2✔
2854

1✔
2855
    auto c1 = origin->add_column_list(*target, "linklist");
2✔
2856
    target->add_column(type_Int, "int");
2✔
2857
    auto c2 = origin->add_column(*target, "link");
2✔
2858

1✔
2859
    Obj t = target->create_object();
2✔
2860
    Obj o = origin->create_object();
2✔
2861
    o.set(c2, t.get_key());
2✔
2862
    LnkLst l = o.get_linklist(c1);
2✔
2863
    l.add(t.get_key());
2✔
2864
    group->commit_and_continue_as_read();
2✔
2865

1✔
2866
    group->promote_to_write();
2✔
2867
    CHECK_EQUAL(1, l.size());
2✔
2868
    target->clear();
2✔
2869
    CHECK_EQUAL(0, l.size());
2✔
2870

1✔
2871
    group->rollback_and_continue_as_read();
2✔
2872
    CHECK_EQUAL(1, l.size());
2✔
2873
}
2✔
2874

2875
TEST(LangBindHelper_RollbackAndContinueAsRead_IntIndex)
2876
{
2✔
2877
    SHARED_GROUP_TEST_PATH(path);
2✔
2878
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2879
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2880
    auto g = sg->start_read();
2✔
2881
    g->promote_to_write();
2✔
2882

1✔
2883
    TableRef target = g->add_table("target");
2✔
2884
    ColKey col = target->add_column(type_Int, "pk");
2✔
2885
    target->add_search_index(col);
2✔
2886

1✔
2887
    std::vector<ObjKey> keys;
2✔
2888
    target->create_objects(REALM_MAX_BPNODE_SIZE + 1, keys);
2✔
2889
    g->commit_and_continue_as_read();
2✔
2890
    g->promote_to_write();
2✔
2891

1✔
2892
    // Ensure that the index has a different bptree layout so that failing to
1✔
2893
    // refresh it will do bad things
1✔
2894
    auto it = target->begin();
2✔
2895
    for (int i = 0; i < REALM_MAX_BPNODE_SIZE + 1; ++i) {
2,004✔
2896
        it->set<int64_t>(col, i);
2,002✔
2897
        ++it;
2,002✔
2898
    }
2,002✔
2899

1✔
2900
    g->rollback_and_continue_as_read();
2✔
2901
    g->promote_to_write();
2✔
2902

1✔
2903
    // Crashes if index has an invalid parent ref
1✔
2904
    target->clear();
2✔
2905
}
2✔
2906

2907

2908
TEST(LangBindHelper_ImplicitTransactions_OverSharedGroupDestruction)
2909
{
2✔
2910
    SHARED_GROUP_TEST_PATH(path);
2✔
2911
    // we hold on to write log collector and registry across a complete
1✔
2912
    // shutdown/initialization of shared rt->
1✔
2913
    std::unique_ptr<Replication> hist1(make_in_realm_history());
2✔
2914
    {
2✔
2915
        DBRef sg = DB::create(*hist1, path, DBOptions(crypt_key()));
2✔
2916
        {
2✔
2917
            WriteTransaction wt(sg);
2✔
2918
            TableRef tr = wt.add_table("table");
2✔
2919
            tr->add_column(type_Int, "first");
2✔
2920
            for (int i = 0; i < 20; i++)
42✔
2921
                tr->create_object();
40✔
2922
            wt.commit();
2✔
2923
        }
2✔
2924
        // no valid shared group anymore
1✔
2925
    }
2✔
2926
    {
2✔
2927
        std::unique_ptr<Replication> hist2(make_in_realm_history());
2✔
2928
        DBRef sg = DB::create(*hist2, path, DBOptions(crypt_key()));
2✔
2929
        {
2✔
2930
            WriteTransaction wt(sg);
2✔
2931
            TableRef tr = wt.get_table("table");
2✔
2932
            for (int i = 0; i < 20; i++)
42✔
2933
                tr->create_object();
40✔
2934
            wt.commit();
2✔
2935
        }
2✔
2936
    }
2✔
2937
}
2✔
2938

2939
TEST(LangBindHelper_ImplicitTransactions_LinkList)
2940
{
2✔
2941
    SHARED_GROUP_TEST_PATH(path);
2✔
2942
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2943
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2944
    auto group = sg->start_write();
2✔
2945
    TableRef origin = group->add_table("origin");
2✔
2946
    TableRef target = group->add_table("target");
2✔
2947
    auto col = origin->add_column_list(*target, "");
2✔
2948
    target->add_column(type_Int, "");
2✔
2949
    auto O0 = origin->create_object();
2✔
2950
    auto T0 = target->create_object();
2✔
2951
    auto link_list = O0.get_linklist(col);
2✔
2952
    link_list.add(T0.get_key());
2✔
2953
    group->commit_and_continue_as_read();
2✔
2954
    group->verify();
2✔
2955
}
2✔
2956

2957

2958
TEST(LangBindHelper_ImplicitTransactions_StringIndex)
2959
{
2✔
2960
    SHARED_GROUP_TEST_PATH(path);
2✔
2961
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2962
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2963
    auto group = sg->start_write();
2✔
2964
    TableRef table = group->add_table("a");
2✔
2965
    auto col = table->add_column(type_String, "b");
2✔
2966
    table->add_search_index(col);
2✔
2967
    group->verify();
2✔
2968
    group->commit_and_continue_as_read();
2✔
2969
    group->verify();
2✔
2970
}
2✔
2971

2972

2973
namespace {
2974

2975
// Test that multiple trackers (of changes) always see a consistent picture.
2976
// This is done by having multiple writers update a table A, query it, and store
2977
// the count of the query in another table, B. Multiple readers then track the
2978
// changes and verify that the count on their query is consistent with the count
2979
// they read from table B. Terminate verification by signalling through a third
2980
// table, C, after a chosen number of yields, which should allow readers to
2981
// run their verification.
2982
void multiple_trackers_writer_thread(DBRef db)
2983
{
14✔
2984
    // insert new random values
7✔
2985
    Random random(random_int<unsigned long>());
14✔
2986
    for (int i = 0; i < 10; ++i) {
154✔
2987
        WriteTransaction wt(db);
140✔
2988
        auto ta = wt.get_table("A");
140✔
2989
        auto col = ta->get_column_keys()[0];
140✔
2990
        for (auto it = ta->begin(); it != ta->end(); ++it) {
28,140✔
2991
            auto e = *it;
28,000✔
2992
            e.set(col, random.draw_int_mod(200));
28,000✔
2993
        }
28,000✔
2994
        auto tb = wt.get_table("B");
140✔
2995
        auto count = ta->where().greater(col, 100).count();
140✔
2996
        tb->begin()->set<int64_t>(tb->get_column_keys()[0], count);
140✔
2997
        wt.commit();
140✔
2998
    }
140✔
2999
}
14✔
3000

3001
void multiple_trackers_reader_thread(TestContext& test_context, DBRef db)
3002
{
6✔
3003
    // verify that consistency is maintained as we advance_read through a
3✔
3004
    // stream of transactions
3✔
3005
    auto g = db->start_read();
6✔
3006
    auto ta = g->get_table("A");
6✔
3007
    auto tb = g->get_table("B");
6✔
3008
    auto tc = g->get_table("C");
6✔
3009
    auto col = ta->get_column_keys()[0];
6✔
3010
    auto b_col = tb->get_column_keys()[0];
6✔
3011
    TableView tv = ta->where().greater(col, 100).find_all();
6✔
3012
    const auto wait_start = std::chrono::steady_clock::now();
6✔
3013
    std::chrono::seconds max_wait_seconds = std::chrono::seconds(1050);
6✔
3014
    while (tc->size() == 0) {
32,198✔
3015
        auto count = tb->begin()->get<int64_t>(b_col);
32,192✔
3016
        tv.sync_if_needed();
32,192✔
3017
        CHECK_EQUAL(tv.size(), count);
32,192✔
3018
        std::this_thread::yield();
32,192✔
3019
        g->advance_read();
32,192✔
3020
        if (std::chrono::steady_clock::now() - wait_start > max_wait_seconds) {
32,192✔
3021
            // if there is a fatal problem with a writer process we don't want the
3022
            // readers to wait forever as a spawned background processs
3023
            constexpr bool reader_process_timed_out = false;
×
3024
            REALM_ASSERT(reader_process_timed_out);
×
3025
        }
×
3026
    }
32,192✔
3027
}
6✔
3028

3029
} // anonymous namespace
3030

3031
TEST(LangBindHelper_ImplicitTransactions_MultipleTrackers)
3032
{
2✔
3033
    const int write_thread_count = 7;
2✔
3034
    const int read_thread_count = 3;
2✔
3035

1✔
3036
    SHARED_GROUP_TEST_PATH(path);
2✔
3037

1✔
3038
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3039
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3040
    {
2✔
3041
        // initialize table with 200 entries holding 0..200
1✔
3042
        WriteTransaction wt(sg);
2✔
3043
        TableRef tr = wt.add_table("A");
2✔
3044
        auto col = tr->add_column(type_Int, "first");
2✔
3045
        for (int j = 0; j < 200; j++) {
402✔
3046
            tr->create_object().set(col, j);
400✔
3047
        }
400✔
3048
        auto table_b = wt.add_table("B");
2✔
3049
        table_b->add_column(type_Int, "bussemand");
2✔
3050
        table_b->create_object().set_all(99);
2✔
3051
        wt.add_table("C");
2✔
3052
        wt.commit();
2✔
3053
    }
2✔
3054
    // FIXME: Use separate arrays for reader and writer threads for safety and readability.
1✔
3055
    Thread threads[write_thread_count + read_thread_count];
2✔
3056
    for (int i = 0; i < write_thread_count; ++i)
16✔
3057
        threads[i].start([&] {
14✔
3058
            multiple_trackers_writer_thread(sg);
13✔
3059
        });
13✔
3060
    std::this_thread::yield();
2✔
3061
    for (int i = 0; i < read_thread_count; ++i) {
8✔
3062
        threads[write_thread_count + i].start([&] {
6✔
3063
            multiple_trackers_reader_thread(test_context, sg);
6✔
3064
        });
6✔
3065
    }
6✔
3066

1✔
3067
    // Wait for all writer threads to complete
1✔
3068
    for (int i = 0; i < write_thread_count; ++i)
16✔
3069
        threads[i].join();
14✔
3070

1✔
3071
    // Allow readers time to catch up
1✔
3072
    for (int k = 0; k < 100; ++k)
202✔
3073
        std::this_thread::yield();
200✔
3074

1✔
3075
    // signal to all readers to complete
1✔
3076
    {
2✔
3077
        WriteTransaction wt(sg);
2✔
3078
        TableRef tr = wt.get_table("C");
2✔
3079
        tr->create_object();
2✔
3080
        wt.commit();
2✔
3081
    }
2✔
3082
    // Wait for all reader threads to complete
1✔
3083
    for (int i = 0; i < read_thread_count; ++i)
8✔
3084
        threads[write_thread_count + i].join();
6✔
3085
}
2✔
3086

3087
// Interprocess communication does not work with encryption enabled on Apple.
3088
// This is because fork() does not play well with Apple primitives such as
3089
// dispatch_queue_t in ReclaimerThreadStopper. This could possibly be fixed if
3090
// we need more tests like this.
3091

3092
#if !REALM_ANDROID && !REALM_IOS
3093

3094
// fork should not be used on android or ios.
3095
// This test must be non-concurrant due to fork. If a child process
3096
// is created while a static mutex is locked (eg. util::GlobalRandom::m_mutex)
3097
// then any attempt to use the mutex would hang infinitely and the child would
3098
// crash upon exit(0) when attempting to destroy a locked mutex.
3099
// This is not run with ASAN because children intentionally call exit(0) which does not
3100
// invoke destructors.
3101
NONCONCURRENT_TEST_IF(LangBindHelper_ImplicitTransactions_InterProcess, testing_supports_spawn_process)
3102
{
2✔
3103
    const int write_process_count = 7;
2✔
3104
    const int read_process_count = 3;
2✔
3105

1✔
3106
    std::vector<std::unique_ptr<SpawnedProcess>> readers;
2✔
3107
    std::vector<std::unique_ptr<SpawnedProcess>> writers;
2✔
3108
    SHARED_GROUP_TEST_PATH(path);
2✔
3109
    auto key = crypt_key();
2✔
3110
    auto process = test_util::spawn_process(test_context.test_details.test_name, "populate");
2✔
3111
    if (process->is_child()) {
2✔
3112
        try {
×
3113
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
3114
            DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3115
            // initialize table with 200 entries holding 0..200
3116
            WriteTransaction wt(sg);
×
3117
            TableRef tr = wt.add_table("A");
×
3118
            auto col = tr->add_column(type_Int, "first");
×
3119
            for (int j = 0; j < 200; j++) {
×
3120
                tr->create_object().set(col, j);
×
3121
            }
×
3122
            auto table_b = wt.add_table("B");
×
3123
            table_b->add_column(type_Int, "bussemand");
×
3124
            table_b->create_object().set_all(99);
×
3125
            wt.add_table("C");
×
3126
            wt.commit();
×
3127
        }
×
3128
        catch (const std::exception& e) {
×
3129
            REALM_ASSERT_EX(false, e.what());
×
3130
            static_cast<void>(e); // e is unused without assertions on
×
3131
        }
×
3132
        exit(0);
×
3133
    }
2✔
3134

1✔
3135
    if (process->is_parent()) {
2✔
3136
        process->wait_for_child_to_finish();
2✔
3137
    }
2✔
3138

1✔
3139
    // intialization complete. Start writers:
1✔
3140
    for (int i = 0; i < write_process_count; ++i) {
16✔
3141
        writers.push_back(
14✔
3142
            test_util::spawn_process(test_context.test_details.test_name, util::format("writer[%1]", i)));
14✔
3143
        if (writers.back()->is_child()) {
14✔
3144
            {
×
3145
                // util::format(std::cout, "Writer[%1](%2) starting.\n", test_util::get_pid(), i);
3146
                std::unique_ptr<Replication> hist(make_in_realm_history());
×
3147
                DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3148
                multiple_trackers_writer_thread(sg);
×
3149
                // util::format(std::cout, "Writer[%1](%2) done.\n", test_util::get_pid(), i);
3150
            } // clean up sg before exit
×
3151
            exit(0);
×
3152
        }
×
3153
    }
14✔
3154
    std::this_thread::yield();
2✔
3155
    // then start readers:
1✔
3156
    for (int i = 0; i < read_process_count; ++i) {
8✔
3157
        readers.push_back(
6✔
3158
            test_util::spawn_process(test_context.test_details.test_name, util::format("reader[%1]", i)));
6✔
3159
        if (readers[i]->is_child()) {
6✔
3160
            {
×
3161
                // util::format(std::cout, "Reader[%1](%2) starting.\n", test_util::get_pid(), i);
3162
                std::unique_ptr<Replication> hist(make_in_realm_history());
×
3163
                DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3164
                multiple_trackers_reader_thread(test_context, sg);
×
3165
                // util::format(std::cout, "Reader[%1] done.\n", i);
3166
            } // clean up sg before exit
×
3167
            exit(0);
×
3168
        }
×
3169
    }
6✔
3170

1✔
3171
    if (process->is_parent()) {
2✔
3172
        // Wait for all writer threads to complete
1✔
3173
        for (int i = 0; i < write_process_count; ++i) {
16✔
3174
            writers[i]->wait_for_child_to_finish();
14✔
3175
        }
14✔
3176

1✔
3177
        // Allow readers time to catch up
1✔
3178
        for (int k = 0; k < 100; ++k)
202✔
3179
            std::this_thread::yield();
200✔
3180

1✔
3181
        // signal to all readers to complete
1✔
3182
        {
2✔
3183
            std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3184
            DBRef sg = DB::create(*hist, path, DBOptions(key));
2✔
3185
            WriteTransaction wt(sg);
2✔
3186
            TableRef tr = wt.get_table("C");
2✔
3187
            tr->create_object();
2✔
3188
            wt.commit();
2✔
3189
        }
2✔
3190

1✔
3191
        // Wait for all reader threads to complete
1✔
3192
        for (int i = 0; i < read_process_count; ++i) {
8✔
3193
            readers[i]->wait_for_child_to_finish();
6✔
3194
        }
6✔
3195
    }
2✔
3196
}
2✔
3197

3198
#endif // !REALM_ANDROID && !REALM_IOS
3199

3200
TEST(LangBindHelper_ImplicitTransactions_NoExtremeFileSpaceLeaks)
3201
{
2✔
3202
    SHARED_GROUP_TEST_PATH(path);
2✔
3203

1✔
3204
    for (int i = 0; i < 100; ++i) {
202✔
3205
        std::unique_ptr<Replication> hist(make_in_realm_history());
200✔
3206
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
200✔
3207
        auto trans = sg->start_read();
200✔
3208
        trans->promote_to_write();
200✔
3209
        trans->commit_and_continue_as_read();
200✔
3210
    }
200✔
3211

1✔
3212
// the miminum filesize (after a commit) is one or two pages, depending on the
1✔
3213
// page size.
1✔
3214
#if REALM_ENABLE_ENCRYPTION
2✔
3215
    if (crypt_key())
2✔
3216
        // Encrypted files are always at least a 4096 byte header plus payload
1✔
3217
        CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size() + 4096);
1✔
3218
    else
2✔
3219
        CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size());
2✔
3220
#else
3221
    CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size());
3222
#endif // REALM_ENABLE_ENCRYPTION
3223
}
2✔
3224

3225

3226
TEST(LangBindHelper_ImplicitTransactions_ContinuedUseOfTable)
3227
{
2✔
3228
    SHARED_GROUP_TEST_PATH(path);
2✔
3229

1✔
3230
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3231
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3232
    auto group = sg->start_read();
2✔
3233
    auto group_w = sg->start_write();
2✔
3234

1✔
3235
    TableRef table_w = group_w->add_table("table");
2✔
3236
    auto col = table_w->add_column(type_Int, "");
2✔
3237
    auto obj = table_w->create_object();
2✔
3238
    group_w->commit_and_continue_as_read();
2✔
3239
    group_w->verify();
2✔
3240

1✔
3241
    group->advance_read();
2✔
3242
    ConstTableRef table = group->get_table("table");
2✔
3243
    CHECK_EQUAL(1, table->size());
2✔
3244
    group->verify();
2✔
3245

1✔
3246
    group_w->promote_to_write();
2✔
3247
    obj.set<int64_t>(col, 1);
2✔
3248
    group_w->commit_and_continue_as_read();
2✔
3249
    group_w->verify();
2✔
3250

1✔
3251
    group->advance_read();
2✔
3252
    auto obj2 = group->import_copy_of(obj);
2✔
3253
    CHECK_EQUAL(1, obj2.get<int64_t>(col));
2✔
3254
    group->verify();
2✔
3255
}
2✔
3256

3257

3258
TEST(LangBindHelper_ImplicitTransactions_ContinuedUseOfLinkList)
3259
{
2✔
3260
    SHARED_GROUP_TEST_PATH(path);
2✔
3261

1✔
3262
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3263
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3264
    auto group = sg->start_read();
2✔
3265
    auto group_w = sg->start_write();
2✔
3266

1✔
3267
    TableRef table_w = group_w->add_table("table");
2✔
3268
    auto col = table_w->add_column_list(*table_w, "flubber");
2✔
3269
    auto obj = table_w->create_object();
2✔
3270
    auto link_list_w = obj.get_linklist(col);
2✔
3271
    link_list_w.add(obj.get_key());
2✔
3272
    // CHECK_EQUAL(1, link_list_w.size()); // avoid this, it hides missing updates
1✔
3273
    group_w->commit_and_continue_as_read();
2✔
3274
    group_w->verify();
2✔
3275

1✔
3276
    group->advance_read();
2✔
3277
    auto link_list = obj.get_linklist(col);
2✔
3278
    CHECK_EQUAL(1, link_list.size());
2✔
3279
    group->verify();
2✔
3280

1✔
3281
    group_w->promote_to_write();
2✔
3282
    // CHECK_EQUAL(1, link_list_w.size()); // avoid this, it hides missing updates
1✔
3283
    link_list_w.add(obj.get_key());
2✔
3284
    CHECK_EQUAL(2, link_list_w.size());
2✔
3285
    group_w->commit_and_continue_as_read();
2✔
3286
    group_w->verify();
2✔
3287

1✔
3288
    group->advance_read();
2✔
3289
    CHECK_EQUAL(2, link_list.size());
2✔
3290
    group->verify();
2✔
3291
}
2✔
3292

3293

3294
TEST(LangBindHelper_MemOnly)
3295
{
2✔
3296
    SHARED_GROUP_TEST_PATH(path);
2✔
3297
    ShortCircuitHistory hist;
2✔
3298
    DBRef sg = DB::create(hist, path, DBOptions(DBOptions::Durability::MemOnly));
2✔
3299

1✔
3300
    // Verify that the db is empty after populating and then re-opening a file
1✔
3301
    {
2✔
3302
        WriteTransaction wt(sg);
2✔
3303
        wt.add_table("table");
2✔
3304
        wt.commit();
2✔
3305
    }
2✔
3306
    {
2✔
3307
        TransactionRef rt = sg->start_read();
2✔
3308
        CHECK(!rt->is_empty());
2✔
3309
    }
2✔
3310
    sg->close();
2✔
3311
    sg = DB::create(hist, path, DBOptions(DBOptions::Durability::MemOnly));
2✔
3312

1✔
3313
    // Verify that basic replication functionality works
1✔
3314
    auto rt = sg->start_read();
2✔
3315
    {
2✔
3316
        WriteTransaction wt(sg);
2✔
3317
        wt.add_table("table");
2✔
3318
        wt.commit();
2✔
3319
    }
2✔
3320

1✔
3321
    CHECK(rt->is_empty());
2✔
3322
    rt->advance_read();
2✔
3323
    CHECK(!rt->is_empty());
2✔
3324
}
2✔
3325

3326
TEST(LangBindHelper_ImplicitTransactions_SearchIndex)
3327
{
2✔
3328
    SHARED_GROUP_TEST_PATH(path);
2✔
3329

1✔
3330
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3331
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3332
    auto rt = sg->start_read();
2✔
3333
    auto group_w = sg->start_read();
2✔
3334

1✔
3335
    // Add initial data
1✔
3336
    group_w->promote_to_write();
2✔
3337
    TableRef table_w = group_w->add_table("table");
2✔
3338
    auto c0 = table_w->add_column(type_Int, "int1");
2✔
3339
    auto c1 = table_w->add_column(type_String, "str");
2✔
3340
    auto c2 = table_w->add_column(type_Int, "int2");
2✔
3341
    auto ok = table_w->create_object(ObjKey{}, {{c1, "2"}, {c0, 1}, {c2, 3}}).get_key();
2✔
3342
    group_w->commit_and_continue_as_read();
2✔
3343
    group_w->verify();
2✔
3344

1✔
3345
    rt->advance_read();
2✔
3346
    ConstTableRef table = rt->get_table("table");
2✔
3347
    auto obj = table->get_object(ok);
2✔
3348
    CHECK_EQUAL(1, obj.get<int64_t>(c0));
2✔
3349
    CHECK_EQUAL("2", obj.get<StringData>(c1));
2✔
3350
    CHECK_EQUAL(3, obj.get<int64_t>(c2));
2✔
3351
    rt->verify();
2✔
3352

1✔
3353
    // Add search index and re-verify
1✔
3354
    group_w->promote_to_write();
2✔
3355
    table_w->add_search_index(c1);
2✔
3356
    group_w->commit_and_continue_as_read();
2✔
3357
    group_w->verify();
2✔
3358

1✔
3359
    rt->advance_read();
2✔
3360
    CHECK_EQUAL(1, obj.get<int64_t>(c0));
2✔
3361
    CHECK_EQUAL("2", obj.get<StringData>(c1));
2✔
3362
    CHECK_EQUAL(3, obj.get<int64_t>(c2));
2✔
3363
    CHECK(table->has_search_index(c1));
2✔
3364
    rt->verify();
2✔
3365

1✔
3366
    // Remove search index and re-verify
1✔
3367
    group_w->promote_to_write();
2✔
3368
    table_w->remove_search_index(c1);
2✔
3369
    group_w->commit_and_continue_as_read();
2✔
3370
    group_w->verify();
2✔
3371

1✔
3372
    rt->advance_read();
2✔
3373
    CHECK_EQUAL(1, obj.get<int64_t>(c0));
2✔
3374
    CHECK_EQUAL("2", obj.get<StringData>(c1));
2✔
3375
    CHECK_EQUAL(3, obj.get<int64_t>(c2));
2✔
3376
    CHECK(!table->has_search_index(c1));
2✔
3377
    rt->verify();
2✔
3378
}
2✔
3379

3380
TEST(LangBindHelper_HandoverQuery)
3381
{
2✔
3382
    SHARED_GROUP_TEST_PATH(path);
2✔
3383
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3384
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3385
    TransactionRef rt = sg->start_read();
2✔
3386
    {
2✔
3387
        WriteTransaction wt(sg);
2✔
3388
        Group& group_w = wt.get_group();
2✔
3389
        TableRef t = group_w.add_table("table2");
2✔
3390
        t->add_column(type_String, "first");
2✔
3391
        auto int_col = t->add_column(type_Int, "second");
2✔
3392
        for (int i = 0; i < 100; ++i) {
202✔
3393
            t->create_object().set(int_col, i);
200✔
3394
        }
200✔
3395
        wt.commit();
2✔
3396
    }
2✔
3397
    rt->advance_read();
2✔
3398
    auto table = rt->get_table("table2");
2✔
3399
    auto int_col = table->get_column_key("second");
2✔
3400
    Query query = table->column<Int>(int_col) < 50;
2✔
3401
    size_t count = query.count();
2✔
3402
    // CHECK(query.is_in_sync());
1✔
3403
    auto vtrans = rt->duplicate();
2✔
3404
    std::unique_ptr<Query> q2 = vtrans->import_copy_of(query, PayloadPolicy::Move);
2✔
3405
    CHECK_EQUAL(count, 50);
2✔
3406
    {
2✔
3407
        // Delete first column. This alters the index of 'second' column
1✔
3408
        WriteTransaction wt(sg);
2✔
3409
        Group& group_w = wt.get_group();
2✔
3410
        TableRef t = group_w.get_table("table2");
2✔
3411
        auto str_col = table->get_column_key("first");
2✔
3412
        t->remove_column(str_col);
2✔
3413
        wt.commit();
2✔
3414
    }
2✔
3415
    rt->advance_read();
2✔
3416
    count = query.count();
2✔
3417
    CHECK_EQUAL(count, 50);
2✔
3418
    count = q2->count();
2✔
3419
    CHECK_EQUAL(count, 50);
2✔
3420
}
2✔
3421

3422
TEST(LangBindHelper_SubqueryHandoverQueryCreatedFromDeletedLinkView)
3423
{
2✔
3424
    SHARED_GROUP_TEST_PATH(path);
2✔
3425
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3426
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3427
    TransactionRef reader;
2✔
3428
    auto writer = sg->start_write();
2✔
3429
    {
2✔
3430
        TableView tv1;
2✔
3431
        auto table = writer->add_table("table");
2✔
3432
        auto table2 = writer->add_table("table2");
2✔
3433
        table2->add_column(type_Int, "int");
2✔
3434
        auto key = table2->create_object().set_all(42).get_key();
2✔
3435

1✔
3436
        auto col = table->add_column_list(*table2, "first");
2✔
3437
        auto obj = table->create_object();
2✔
3438
        auto link_view = obj.get_linklist(col);
2✔
3439

1✔
3440
        link_view.add(key);
2✔
3441
        writer->commit_and_continue_as_read();
2✔
3442

1✔
3443
        Query qq = table2->where(link_view);
2✔
3444
        CHECK_EQUAL(qq.count(), 1);
2✔
3445
        writer->promote_to_write();
2✔
3446
        table->clear();
2✔
3447
        writer->commit_and_continue_as_read();
2✔
3448
        CHECK_EQUAL(link_view.size(), 0);
2✔
3449
        CHECK_EQUAL(qq.count(), 0);
2✔
3450

1✔
3451
        reader = writer->duplicate();
2✔
3452
#ifdef OLD_CORE_BEHAVIOR
3453
        // FIXME: Old core would allow the code below, but new core will throw.
3454
        //
3455
        // Why should a query still be valid after a change, when it would not be possible
3456
        // to reconstruct the query from new after said change?
3457
        //
3458
        // In this specific case, the query is constructed from a linkview on an object
3459
        // which is destroyed. After the object is destroyed, the linkview obviously
3460
        // cannot be constructed, and hence the query can also not be constructed.
3461
        auto lv2 = reader->import_copy_of(link_view);
3462
        auto rq = reader->import_copy_of(qq, PayloadPolicy::Copy);
3463
        writer->close();
3464
        auto tv = rq->find_all();
3465

3466
        CHECK(tv.is_in_sync());
3467
        CHECK(tv.is_attached());
3468
        CHECK_EQUAL(0, tv.size());
3469
#endif
3470
    }
2✔
3471
}
2✔
3472

3473

3474
TEST(LangBindHelper_SubqueryHandoverDependentViews)
3475
{
2✔
3476
    SHARED_GROUP_TEST_PATH(path);
2✔
3477
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3478
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3479
    std::unique_ptr<Query> qq2;
2✔
3480
    TransactionRef reader;
2✔
3481
    ColKey col1;
2✔
3482
    {
2✔
3483
        {
2✔
3484
            TableView tv1;
2✔
3485
            auto writer = sg->start_write();
2✔
3486
            TableRef table = writer->add_table("table2");
2✔
3487
            auto col0 = table->add_column(type_Int, "first");
2✔
3488
            col1 = table->add_column(type_Bool, "even");
2✔
3489
            for (int i = 0; i < 100; ++i) {
202✔
3490
                auto obj = table->create_object();
200✔
3491
                obj.set<int>(col0, i);
200✔
3492
                bool isEven = ((i % 2) == 0);
200✔
3493
                obj.set<bool>(col1, isEven);
200✔
3494
            }
200✔
3495
            writer->commit_and_continue_as_read();
2✔
3496
            tv1 = table->where().less_equal(col0, 50).find_all();
2✔
3497
            Query qq = tv1.get_parent()->where(&tv1);
2✔
3498
            reader = writer->duplicate();
2✔
3499
            qq2 = reader->import_copy_of(qq, PayloadPolicy::Copy);
2✔
3500
            CHECK(tv1.is_attached());
2✔
3501
            CHECK_EQUAL(51, tv1.size());
2✔
3502
        }
2✔
3503
        {
2✔
3504
            realm::TableView tv = qq2->equal(col1, true).find_all();
2✔
3505

1✔
3506
            CHECK(tv.is_in_sync());
2✔
3507
            CHECK(tv.is_attached());
2✔
3508
            CHECK_EQUAL(26, tv.size()); // BOOM! fail with 50
2✔
3509
        }
2✔
3510
    }
2✔
3511
}
2✔
3512

3513
TEST(LangBindHelper_HandoverPartialQuery)
3514
{
2✔
3515
    SHARED_GROUP_TEST_PATH(path);
2✔
3516
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3517
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3518
    std::unique_ptr<Query> qq2;
2✔
3519
    TransactionRef reader;
2✔
3520
    ColKey col0;
2✔
3521
    {
2✔
3522
        {
2✔
3523
            TableView tv1;
2✔
3524
            auto writer = sg->start_write();
2✔
3525
            TableRef table = writer->add_table("table2");
2✔
3526
            col0 = table->add_column(type_Int, "first");
2✔
3527
            auto col1 = table->add_column(type_Bool, "even");
2✔
3528
            for (int i = 0; i < 100; ++i) {
202✔
3529
                auto obj = table->create_object();
200✔
3530
                obj.set<int>(col0, i);
200✔
3531
                bool isEven = ((i % 2) == 0);
200✔
3532
                obj.set<bool>(col1, isEven);
200✔
3533
            }
200✔
3534
            writer->commit_and_continue_as_read();
2✔
3535
            tv1 = table->where().less_equal(col0, 50).find_all();
2✔
3536
            Query qq = tv1.get_parent()->where(&tv1);
2✔
3537
            reader = writer->duplicate();
2✔
3538
            qq2 = reader->import_copy_of(qq, PayloadPolicy::Copy);
2✔
3539
            CHECK(tv1.is_attached());
2✔
3540
            CHECK_EQUAL(51, tv1.size());
2✔
3541
        }
2✔
3542
        {
2✔
3543
            TableView tv = qq2->greater(col0, 48).find_all();
2✔
3544
            CHECK(tv.is_attached());
2✔
3545
            CHECK_EQUAL(2, tv.size());
2✔
3546
            auto obj = tv.get_object(0);
2✔
3547
            CHECK_EQUAL(49, obj.get<int64_t>(col0));
2✔
3548
            obj = tv.get_object(1);
2✔
3549
            CHECK_EQUAL(50, obj.get<int64_t>(col0));
2✔
3550
        }
2✔
3551
    }
2✔
3552
}
2✔
3553

3554

3555
// Verify that an in-sync TableView backed by a Query that is restricted to a TableView
3556
// remains in sync when handed-over using a mutable payload.
3557
TEST(LangBindHelper_HandoverNestedTableViews)
3558
{
2✔
3559
    SHARED_GROUP_TEST_PATH(path);
2✔
3560
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3561
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3562
    {
2✔
3563
        TransactionRef reader;
2✔
3564
        std::unique_ptr<TableView> tv;
2✔
3565
        {
2✔
3566
            auto writer = sg->start_write();
2✔
3567
            TableRef table = writer->add_table("table2");
2✔
3568
            auto col = table->add_column(type_Int, "first");
2✔
3569
            for (int i = 0; i < 100; ++i) {
202✔
3570
                table->create_object().set_all(i);
200✔
3571
            }
200✔
3572
            writer->commit_and_continue_as_read();
2✔
3573
            // Create a TableView tv2 that is backed by a Query that is restricted to rows from TableView tv1.
1✔
3574
            TableView tv1 = table->where().less_equal(col, 50).find_all();
2✔
3575
            TableView tv2 = tv1.get_parent()->where(&tv1).greater(col, 25).find_all();
2✔
3576
            CHECK(tv2.is_in_sync());
2✔
3577
            reader = writer->duplicate();
2✔
3578
            tv = reader->import_copy_of(tv2, PayloadPolicy::Move);
2✔
3579
        }
2✔
3580
        CHECK(tv->is_in_sync());
2✔
3581
        CHECK(tv->is_attached());
2✔
3582
        CHECK_EQUAL(25, tv->size());
2✔
3583
    }
2✔
3584
}
2✔
3585

3586

3587
TEST(LangBindHelper_HandoverAccessors)
3588
{
2✔
3589
    SHARED_GROUP_TEST_PATH(path);
2✔
3590
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3591
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3592
    TransactionRef reader;
2✔
3593
    ColKey col;
2✔
3594
    std::unique_ptr<TableView> tv2;
2✔
3595
    std::unique_ptr<TableView> tv3;
2✔
3596
    std::unique_ptr<TableView> tv4;
2✔
3597
    std::unique_ptr<TableView> tv5;
2✔
3598
    std::unique_ptr<TableView> tv6;
2✔
3599
    std::unique_ptr<TableView> tv7;
2✔
3600
    {
2✔
3601
        TableView tv;
2✔
3602
        auto writer = sg->start_write();
2✔
3603
        TableRef table = writer->add_table("table2");
2✔
3604
        col = table->add_column(type_Int, "first");
2✔
3605
        for (int i = 0; i < 100; ++i) {
202✔
3606
            table->create_object().set_all(i);
200✔
3607
        }
200✔
3608
        writer->commit_and_continue_as_read();
2✔
3609

1✔
3610
        tv = table->where().find_all();
2✔
3611
        CHECK(tv.is_attached());
2✔
3612
        CHECK_EQUAL(100, tv.size());
2✔
3613
        for (int i = 0; i < 100; ++i)
202✔
3614
            CHECK_EQUAL(i, tv.get_object(i).get<Int>(col));
200✔
3615

1✔
3616
        reader = writer->duplicate();
2✔
3617
        tv2 = reader->import_copy_of(tv, PayloadPolicy::Copy);
2✔
3618
        CHECK(tv.is_attached());
2✔
3619
        CHECK(tv.is_in_sync());
2✔
3620

1✔
3621
        tv3 = reader->import_copy_of(tv, PayloadPolicy::Stay);
2✔
3622
        CHECK(tv.is_attached());
2✔
3623
        CHECK(tv.is_in_sync());
2✔
3624

1✔
3625
        tv4 = reader->import_copy_of(tv, PayloadPolicy::Move);
2✔
3626
        CHECK(tv.is_attached());
2✔
3627
        CHECK(!tv.is_in_sync());
2✔
3628

1✔
3629
        // and again, but this time with the source out of sync:
1✔
3630
        tv5 = reader->import_copy_of(tv, PayloadPolicy::Copy);
2✔
3631
        CHECK(tv.is_attached());
2✔
3632
        CHECK(!tv.is_in_sync());
2✔
3633

1✔
3634
        tv6 = reader->import_copy_of(tv, PayloadPolicy::Stay);
2✔
3635
        CHECK(tv.is_attached());
2✔
3636
        CHECK(!tv.is_in_sync());
2✔
3637

1✔
3638
        tv7 = reader->import_copy_of(tv, PayloadPolicy::Move);
2✔
3639
        CHECK(tv.is_attached());
2✔
3640
        CHECK(!tv.is_in_sync());
2✔
3641

1✔
3642
        // and verify, that even though it was out of sync, we can bring it in sync again
1✔
3643
        tv.sync_if_needed();
2✔
3644
        CHECK(tv.is_in_sync());
2✔
3645

1✔
3646
        // Obj handover tested elsewhere
1✔
3647
    }
2✔
3648
    {
2✔
3649
        // now examining stuff handed over to other transaction
1✔
3650
        // with payload:
1✔
3651
        CHECK(tv2->is_attached());
2✔
3652
        CHECK(tv2->is_in_sync());
2✔
3653
        CHECK_EQUAL(100, tv2->size());
2✔
3654
        for (int i = 0; i < 100; ++i)
202✔
3655
            CHECK_EQUAL(i, tv2->get_object(i).get<Int>(col));
200✔
3656
        // importing one without payload:
1✔
3657
        CHECK(tv3->is_attached());
2✔
3658
        CHECK(!tv3->is_in_sync());
2✔
3659
        tv3->sync_if_needed();
2✔
3660
        CHECK_EQUAL(100, tv3->size());
2✔
3661
        for (int i = 0; i < 100; ++i)
202✔
3662
            CHECK_EQUAL(i, tv3->get_object(i).get<Int>(col));
200✔
3663

1✔
3664
        // one with payload:
1✔
3665
        CHECK(tv4->is_attached());
2✔
3666
        CHECK(tv4->is_in_sync());
2✔
3667
        CHECK_EQUAL(100, tv4->size());
2✔
3668
        for (int i = 0; i < 100; ++i)
202✔
3669
            CHECK_EQUAL(i, tv4->get_object(i).get<Int>(col));
200✔
3670

1✔
3671
        // verify that subsequent imports are all without payload:
1✔
3672
        CHECK(tv5->is_attached());
2✔
3673
        CHECK(!tv5->is_in_sync());
2✔
3674

1✔
3675
        CHECK(tv6->is_attached());
2✔
3676
        CHECK(!tv6->is_in_sync());
2✔
3677

1✔
3678
        CHECK(tv7->is_attached());
2✔
3679
        CHECK(!tv7->is_in_sync());
2✔
3680
    }
2✔
3681
}
2✔
3682

3683
TEST(LangBindHelper_TableViewAndTransactionBoundaries)
3684
{
2✔
3685
    SHARED_GROUP_TEST_PATH(path);
2✔
3686
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3687
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3688
    ColKey col;
2✔
3689
    {
2✔
3690
        WriteTransaction wt(sg);
2✔
3691
        auto table = wt.add_table("myTable");
2✔
3692
        col = table->add_column(type_Int, "myColumn");
2✔
3693
        table->create_object().set_all(42);
2✔
3694
        wt.commit();
2✔
3695
    }
2✔
3696
    auto rt = sg->start_read();
2✔
3697
    auto tv = rt->get_table("myTable")->where().greater(col, 40).find_all();
2✔
3698
    CHECK(tv.is_in_sync());
2✔
3699
    {
2✔
3700
        WriteTransaction wt(sg);
2✔
3701
        wt.commit();
2✔
3702
    }
2✔
3703
    rt->advance_read();
2✔
3704
    CHECK(tv.is_in_sync());
2✔
3705
    {
2✔
3706
        WriteTransaction wt(sg);
2✔
3707
        wt.commit();
2✔
3708
    }
2✔
3709
    rt->promote_to_write();
2✔
3710
    CHECK(tv.is_in_sync());
2✔
3711
    rt->commit_and_continue_as_read();
2✔
3712
    CHECK(tv.is_in_sync());
2✔
3713
    {
2✔
3714
        WriteTransaction wt(sg);
2✔
3715
        auto table = wt.get_table("myTable");
2✔
3716
        table->begin()->set_all(41);
2✔
3717
        wt.commit();
2✔
3718
    }
2✔
3719
    rt->advance_read();
2✔
3720
    CHECK(!tv.is_in_sync());
2✔
3721
    tv.sync_if_needed();
2✔
3722
    CHECK(tv.is_in_sync());
2✔
3723
    rt->advance_read();
2✔
3724
    CHECK(tv.is_in_sync());
2✔
3725
}
2✔
3726

3727
namespace {
3728
// support threads for handover test. The setup is as follows:
3729
// thread A writes a stream of updates to the database,
3730
// thread B listens and continously does advance_read to see the updates.
3731
// thread B also has a table view, which it continuosly keeps in sync in response
3732
// to the updates. It then hands over the result to thread C.
3733
// thread C continuously recieves copies of the results obtained in thead B and
3734
// verifies them (by comparing with its own local, but identical query)
3735

3736
template <typename T>
3737
struct HandoverControl {
3738
    Mutex m_lock;
3739
    CondVar m_changed;
3740
    std::unique_ptr<T> m_handover;
3741
    bool m_has_feedback = false;
3742
    void put(std::unique_ptr<T> h)
3743
    {
1,840✔
3744
        LockGuard lg(m_lock);
1,840✔
3745
        // std::cout << "put " << h << std::endl;
926✔
3746
        while (m_handover != nullptr)
1,840✔
3747
            m_changed.wait(lg);
×
3748
        // std::cout << " -- put " << h << std::endl;
926✔
3749
        m_handover = std::move(h);
1,840✔
3750
        m_changed.notify_all();
1,840✔
3751
    }
1,840✔
3752
    void get(std::unique_ptr<T>& h)
3753
    {
1,840✔
3754
        LockGuard lg(m_lock);
1,840✔
3755
        // std::cout << "get " << std::endl;
926✔
3756
        while (m_handover == nullptr)
2,219✔
3757
            m_changed.wait(lg);
379✔
3758
        // std::cout << " -- get " << m_handover << std::endl;
926✔
3759
        h = std::move(m_handover);
1,840✔
3760
        m_handover = nullptr;
1,840✔
3761
        m_changed.notify_all();
1,840✔
3762
    }
1,840✔
3763
    bool try_get(std::unique_ptr<T>& h)
3764
    {
3765
        LockGuard lg(m_lock);
3766
        if (m_handover == nullptr)
3767
            return false;
3768
        h = std::move(m_handover);
3769
        m_handover = nullptr;
3770
        m_changed.notify_all();
3771
        return true;
3772
    }
3773
    void signal_feedback()
3774
    {
1,840✔
3775
        LockGuard lg(m_lock);
1,840✔
3776
        m_has_feedback = true;
1,840✔
3777
        m_changed.notify_all();
1,840✔
3778
    }
1,840✔
3779
    void wait_feedback()
3780
    {
1,840✔
3781
        LockGuard lg(m_lock);
1,840✔
3782
        while (!m_has_feedback)
3,836✔
3783
            m_changed.wait(lg);
1,996✔
3784
        m_has_feedback = false;
1,840✔
3785
    }
1,840✔
3786
    HandoverControl(const HandoverControl&) = delete;
3787
    HandoverControl() {}
2✔
3788
};
3789

3790
void handover_writer(DBRef db)
3791
{
2✔
3792
    //    std::unique_ptr<Replication> hist(make_in_realm_history());
1✔
3793
    //    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
1✔
3794
    auto g = db->start_read();
2✔
3795
    auto table = g->get_table("table");
2✔
3796
    Random random(random_int<unsigned long>());
2✔
3797
    for (int i = 1; i < 5000; ++i) {
10,000✔
3798
        g->promote_to_write();
9,998✔
3799
        // table holds random numbers >= 1, until the writing process
4,999✔
3800
        // finishes, after n new entry with value 0 is added to signal termination
4,999✔
3801
        table->create_object().set_all(1 + random.draw_int_mod(100));
9,998✔
3802
        g->commit_and_continue_as_read();
9,998✔
3803
        // improve chance of consumers running concurrently with
4,999✔
3804
        // new writes:
4,999✔
3805
        for (int n = 0; n < 10; ++n)
109,978✔
3806
            std::this_thread::yield();
99,980✔
3807
    }
9,998✔
3808
    g->promote_to_write();
2✔
3809
    table->create_object().set_all(0); // <---- signals other threads to stop
2✔
3810
    g->commit();
2✔
3811
}
2✔
3812

3813
struct Work {
3814
    TransactionRef tr;
3815
    std::unique_ptr<TableView> tv;
3816
};
3817

3818
void handover_querier(HandoverControl<Work>* control, TestContext& test_context, DBRef db)
3819
{
2✔
3820
    // We need to ensure that the initial version observed is *before* the final
1✔
3821
    // one written by the writer thread. We do this (simplisticly) by locking on
1✔
3822
    // to the initial version before even starting the writer.
1✔
3823
    auto g = db->start_read();
2✔
3824
    Thread writer;
2✔
3825
    writer.start([&] {
2✔
3826
        handover_writer(db);
2✔
3827
    });
2✔
3828
    TableRef table = g->get_table("table");
2✔
3829
    ColKeys cols = table->get_column_keys();
2✔
3830
    TableView tv = table->where().greater(cols[0], 50).find_all();
2✔
3831
    for (;;) {
1,825,144✔
3832
        // wait here for writer to change the database. Kind of wasteful, but wait_for_change()
167,854✔
3833
        // is not available on osx.
167,854✔
3834
        if (!db->has_changed(g)) {
1,825,144✔
3835
            std::this_thread::yield();
1,823,304✔
3836
            continue;
1,823,304✔
3837
        }
1,823,304✔
3838

926✔
3839
        g->advance_read();
1,840✔
3840
        CHECK(!tv.is_in_sync());
1,840✔
3841
        tv.sync_if_needed();
1,840✔
3842
        CHECK(tv.is_in_sync());
1,840✔
3843
        auto ref = g->duplicate();
1,840✔
3844
        std::unique_ptr<Work> h = std::make_unique<Work>();
1,840✔
3845
        h->tr = ref;
1,840✔
3846
        h->tv = ref->import_copy_of(tv, PayloadPolicy::Move);
1,840✔
3847
        control->put(std::move(h));
1,840✔
3848

926✔
3849
        // here we need to allow the reciever to get hold on the proper version before
926✔
3850
        // we go through the loop again and advance_read().
926✔
3851
        control->wait_feedback();
1,840✔
3852
        std::this_thread::yield();
1,840✔
3853

926✔
3854
        if (table->where().equal(cols[0], 0).count() >= 1)
1,840✔
3855
            break;
2✔
3856
    }
1,840✔
3857
    g->end_read();
2✔
3858
    writer.join();
2✔
3859
}
2✔
3860

3861
void handover_verifier(HandoverControl<Work>* control, TestContext& test_context)
3862
{
2✔
3863
    bool not_done = true;
2✔
3864
    while (not_done) {
1,842✔
3865
        std::unique_ptr<Work> work;
1,840✔
3866
        control->get(work);
1,840✔
3867

926✔
3868
        auto g = work->tr;
1,840✔
3869
        control->signal_feedback();
1,840✔
3870
        TableRef table = g->get_table("table");
1,840✔
3871
        ColKeys cols = table->get_column_keys();
1,840✔
3872
        TableView tv = table->where().greater(cols[0], 50).find_all();
1,840✔
3873
        CHECK(tv.is_in_sync());
1,840✔
3874
        std::unique_ptr<TableView> tv2 = std::move(work->tv);
1,840✔
3875
        CHECK(tv.is_in_sync());
1,840✔
3876
        CHECK(tv2->is_in_sync());
1,840✔
3877
        CHECK_EQUAL(tv.size(), tv2->size());
1,840✔
3878
        for (size_t k = 0; k < tv.size(); ++k) {
1,276,409✔
3879
            auto o = tv.get_object(k);
1,274,569✔
3880
            auto o2 = tv2->get_object(k);
1,274,569✔
3881
            CHECK_EQUAL(o.get<int64_t>(cols[0]), o2.get<int64_t>(cols[0]));
1,274,569✔
3882
        }
1,274,569✔
3883
        if (table->where().equal(cols[0], 0).count() >= 1)
1,840✔
3884
            not_done = false;
2✔
3885
        g->close();
1,840✔
3886
    }
1,840✔
3887
}
2✔
3888

3889
} // anonymous namespace
3890

3891
namespace {
3892

3893
void attacher(std::string path, ColKey col)
3894
{
20✔
3895
    // Creating a new DB in each attacher is on purpose, since we're
10✔
3896
    // testing races in the attachment process, and that only takes place
10✔
3897
    // during creation of the DB object.
10✔
3898
    std::unique_ptr<Replication> hist(make_in_realm_history());
20✔
3899
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
20✔
3900
    for (int i = 0; i < 100; ++i) {
2,020✔
3901
        auto g = sg->start_read();
2,000✔
3902
        g->verify();
2,000✔
3903
        auto table = g->get_table("table");
2,000✔
3904
        g->promote_to_write();
2,000✔
3905
        auto o = table->get_object(ObjKey(i));
2,000✔
3906
        auto o2 = table->get_object(ObjKey(i * 10));
2,000✔
3907
        o.set<int64_t>(col, 1 + o2.get<int64_t>(col));
2,000✔
3908
        g->commit_and_continue_as_read();
2,000✔
3909
        g->verify();
2,000✔
3910
        g->end_read();
2,000✔
3911
    }
2,000✔
3912
}
20✔
3913
} // anonymous namespace
3914

3915

3916
// Disable with TSAN because it needs to synchronize between multiple DBs, and TSAN isn't able to track
3917
// acquire/release across multiple mappings of the same underlying memory.
3918
TEST_IF(LangBindHelper_RacingAttachers, !running_with_tsan)
3919
{
2✔
3920
    const int num_attachers = 10;
2✔
3921
    SHARED_GROUP_TEST_PATH(path);
2✔
3922
    ColKey col;
2✔
3923
    {
2✔
3924
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3925
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3926
        auto g = sg->start_write();
2✔
3927
        auto table = g->add_table("table");
2✔
3928
        col = table->add_column(type_Int, "first");
2✔
3929
        for (int i = 0; i < 1000; ++i)
2,002✔
3930
            table->create_object(ObjKey(i));
2,000✔
3931
        g->commit();
2✔
3932
    }
2✔
3933
    Thread attachers[num_attachers];
2✔
3934
    for (int i = 0; i < num_attachers; ++i) {
22✔
3935
        attachers[i].start([&] {
19✔
3936
            attacher(path, col);
19✔
3937
        });
19✔
3938
    }
20✔
3939
    for (int i = 0; i < num_attachers; ++i) {
22✔
3940
        attachers[i].join();
20✔
3941
    }
20✔
3942
}
2✔
3943

3944
// This test takes a very long time when running with valgrind
3945
TEST_IF(LangBindHelper_HandoverBetweenThreads, !running_with_valgrind)
3946
{
2✔
3947
    SHARED_GROUP_TEST_PATH(path);
2✔
3948
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3949
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3950
    auto g = sg->start_write();
2✔
3951
    auto table = g->add_table("table");
2✔
3952
    table->add_column(type_Int, "first");
2✔
3953
    g->commit();
2✔
3954
    g = sg->start_read();
2✔
3955
    table = g->get_table("table");
2✔
3956
    CHECK(bool(table));
2✔
3957
    g->end_read();
2✔
3958

1✔
3959
    HandoverControl<Work> control;
2✔
3960
    Thread querier, verifier;
2✔
3961
    querier.start([&] {
2✔
3962
        handover_querier(&control, test_context, sg);
2✔
3963
    });
2✔
3964
    verifier.start([&] {
2✔
3965
        handover_verifier(&control, test_context);
2✔
3966
    });
2✔
3967
    querier.join();
2✔
3968
    verifier.join();
2✔
3969
}
2✔
3970

3971

3972
TEST(LangBindHelper_HandoverDependentViews)
3973
{
2✔
3974
    SHARED_GROUP_TEST_PATH(path);
2✔
3975
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3976
    DBRef db = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3977
    TransactionRef tr;
2✔
3978
    std::unique_ptr<TableView> tv_ov;
2✔
3979
    ColKey col;
2✔
3980
    {
2✔
3981
        // Untyped interface
1✔
3982
        {
2✔
3983
            TableView tv1;
2✔
3984
            TableView tv2;
2✔
3985
            auto group_w = db->start_write();
2✔
3986
            TableRef table = group_w->add_table("table2");
2✔
3987
            col = table->add_column(type_Int, "first");
2✔
3988
            for (int i = 0; i < 100; ++i) {
202✔
3989
                table->create_object().set_all(i);
200✔
3990
            }
200✔
3991
            group_w->commit_and_continue_as_read();
2✔
3992
            tv1 = table->where().find_all();
2✔
3993
            tv2 = table->where(&tv1).find_all();
2✔
3994
            CHECK(tv1.is_attached());
2✔
3995
            CHECK(tv2.is_attached());
2✔
3996
            CHECK_EQUAL(100, tv1.size());
2✔
3997
            for (int i = 0; i < 100; ++i) {
202✔
3998
                auto o = tv1.get_object(i);
200✔
3999
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4000
            }
200✔
4001
            CHECK_EQUAL(100, tv2.size());
2✔
4002
            for (int i = 0; i < 100; ++i) {
202✔
4003
                auto o = tv2.get_object(i);
200✔
4004
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4005
            }
200✔
4006
            tr = group_w->duplicate();
2✔
4007
            tv_ov = tr->import_copy_of(tv2, PayloadPolicy::Copy);
2✔
4008
            CHECK(tv1.is_attached());
2✔
4009
            CHECK(tv2.is_attached());
2✔
4010
        }
2✔
4011
        {
2✔
4012
            CHECK(tv_ov->is_in_sync());
2✔
4013
            // CHECK(tv1.is_attached());
1✔
4014
            CHECK(tv_ov->is_attached());
2✔
4015
            CHECK_EQUAL(100, tv_ov->size());
2✔
4016
            for (int i = 0; i < 100; ++i) {
202✔
4017
                auto o = tv_ov->get_object(i);
200✔
4018
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4019
            }
200✔
4020
        }
2✔
4021
    }
2✔
4022
}
2✔
4023

4024

4025
TEST(LangBindHelper_HandoverTableViewWithLnkLst)
4026
{
2✔
4027
    // First iteration hands-over a normal valid attached LnkLst. Second
1✔
4028
    // iteration hands-over a detached LnkLst.
1✔
4029
    for (int detached = 0; detached < 2; detached++) {
6✔
4030
        SHARED_GROUP_TEST_PATH(path);
4✔
4031
        std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
4032
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
4033
        ColKey col_link2, col0;
4✔
4034
        ObjKey ok0, ok1, ok2;
4✔
4035
        TransactionRef tr;
4✔
4036
        std::unique_ptr<TableView> tv2;
4✔
4037
        std::unique_ptr<Query> q2;
4✔
4038
        {
4✔
4039
            TableView tv;
4✔
4040
            auto group_w = sg->start_write();
4✔
4041

2✔
4042
            TableRef table1 = group_w->add_table("table1");
4✔
4043
            TableRef table2 = group_w->add_table("table2");
4✔
4044

2✔
4045
            // add some more columns to table1 and table2
2✔
4046
            col0 = table1->add_column(type_Int, "col1");
4✔
4047
            table1->add_column(type_String, "str1");
4✔
4048

2✔
4049
            // add some rows
2✔
4050
            ok0 = table1->create_object().set_all(300, "delta").get_key();
4✔
4051
            ok1 = table1->create_object().set_all(100, "alfa").get_key();
4✔
4052
            ok2 = table1->create_object().set_all(200, "beta").get_key();
4✔
4053

2✔
4054
            col_link2 = table2->add_column_list(*table1, "linklist");
4✔
4055

2✔
4056
            auto o = table2->create_object();
4✔
4057
            auto lvr = o.get_linklist(col_link2);
4✔
4058
            lvr.clear();
4✔
4059
            lvr.add(ok0);
4✔
4060
            lvr.add(ok1);
4✔
4061
            lvr.add(ok2);
4✔
4062

2✔
4063
            // Return all rows of table1 (the linked-to-table) that match the criteria and is in the LinkList
2✔
4064

2✔
4065
            // q.m_table = table1
2✔
4066
            // q.m_view = lvr
2✔
4067
            Query q = table1->where(lvr).and_query(table1->column<Int>(col0) > 100);
4✔
4068

2✔
4069
            // Remove the LinkList that the query depends on, to see if a detached
2✔
4070
            // LinkList can be handed over correctly
2✔
4071
            if (detached == 1)
4✔
4072
                table2->remove_object(o.get_key());
2✔
4073

2✔
4074
            tv = q.find_all(); // tv = { 0, 2 } (only first iteration)
4✔
4075
            CHECK(tv.is_in_sync());
4✔
4076
            group_w->commit_and_continue_as_read();
4✔
4077
            tr = group_w->duplicate();
4✔
4078
            CHECK(tv.is_in_sync());
4✔
4079
            tv2 = tr->import_copy_of(tv, PayloadPolicy::Copy);
4✔
4080
            q2 = tr->import_copy_of(q, PayloadPolicy::Copy);
4✔
4081
            auto tv3a = q.find_all();
4✔
4082
            auto tv3b = q2->find_all();
4✔
4083
        }
4✔
4084
        {
4✔
4085
            auto tv3 = q2->find_all();
4✔
4086
            CHECK(tv2->is_in_sync());
4✔
4087
            if (detached == 0) {
4✔
4088
                CHECK_EQUAL(2, tv2->size());
2✔
4089
                CHECK_EQUAL(ok0, tv2->get_key(0));
2✔
4090
                CHECK_EQUAL(ok2, tv2->get_key(1));
2✔
4091
                CHECK_EQUAL(2, tv3.size());
2✔
4092
                CHECK_EQUAL(ok0, tv3.get_key(0));
2✔
4093
                CHECK_EQUAL(ok2, tv3.get_key(1));
2✔
4094
            }
2✔
4095
            else {
2✔
4096
                CHECK_EQUAL(0, tv2->size());
2✔
4097
                CHECK_EQUAL(0, tv3.size());
2✔
4098
            }
2✔
4099
            tr->close();
4✔
4100
        }
4✔
4101
    }
4✔
4102
}
2✔
4103

4104

4105
TEST(LangBindHelper_HandoverTableViewWithQueryOnLink)
4106
{
2✔
4107
    for (int detached = 0; detached < 2; detached++) {
6✔
4108
        SHARED_GROUP_TEST_PATH(path);
4✔
4109
        std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
4110
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
4111
        TransactionRef tr;
4✔
4112
        ObjKey target;
4✔
4113
        std::unique_ptr<TableView> tv2;
4✔
4114
        std::unique_ptr<Query> q2;
4✔
4115
        {
4✔
4116
            auto group_w = sg->start_write();
4✔
4117

2✔
4118
            TableRef table1 = group_w->add_table("table1");
4✔
4119
            TableRef table2 = group_w->add_table("table2");
4✔
4120
            table1->add_column(type_Int, "col1");
4✔
4121
            auto col_link = table2->add_column(*table1, "link");
4✔
4122

2✔
4123
            target = table1->create_object().set_all(300).get_key();
4✔
4124
            auto o = table2->create_object().set_all(target);
4✔
4125
            Query q = table2->where().and_query(table2->column<Link>(col_link) == table1->get_object(target));
4✔
4126

2✔
4127
            // Remove the object that the query depends on, to see if a detached
2✔
4128
            // object can be handed over correctly
2✔
4129
            if (detached == 1)
4✔
4130
                table2->remove_object(o.get_key());
2✔
4131

2✔
4132
            auto tv = q.find_all();
4✔
4133
            CHECK(tv.is_in_sync());
4✔
4134
            group_w->commit_and_continue_as_read();
4✔
4135
            tr = group_w->duplicate();
4✔
4136
            CHECK(tv.is_in_sync());
4✔
4137
            tv2 = tr->import_copy_of(tv, PayloadPolicy::Copy);
4✔
4138
            q2 = tr->import_copy_of(q, PayloadPolicy::Copy);
4✔
4139
        }
4✔
4140
        {
4✔
4141
            auto tv3 = q2->find_all();
4✔
4142
            CHECK(tv2->is_in_sync());
4✔
4143
            if (detached == 0) {
4✔
4144
                CHECK_EQUAL(1, tv2->size());
2✔
4145
                CHECK_EQUAL(target, tv2->get_key(0));
2✔
4146
                CHECK_EQUAL(1, tv3.size());
2✔
4147
                CHECK_EQUAL(target, tv3.get_key(0));
2✔
4148
            }
2✔
4149
            else {
2✔
4150
                CHECK_EQUAL(0, tv2->size());
2✔
4151
                CHECK_EQUAL(0, tv3.size());
2✔
4152
            }
2✔
4153
            tr->close();
4✔
4154
        }
4✔
4155
    }
4✔
4156
}
2✔
4157

4158

4159
#ifdef LEGACY_TESTS // (not useful as std unittest)
4160
namespace {
4161

4162
void do_write_work(std::string path, size_t id, size_t num_rows)
4163
{
4164
    const size_t num_iterations = 5000000; // this makes it run for a loooong time
4165
    const size_t payload_length_small = 10;
4166
    const size_t payload_length_large = 5000;   // > 4096 == page_size
4167
    Random random(random_int<unsigned long>()); // Seed from slow global generator
4168
    const char* key = crypt_key(true);
4169
    for (size_t rep = 0; rep < num_iterations; ++rep) {
4170
        std::unique_ptr<Replication> hist(make_in_realm_history());
4171
        DBRef sg = DB::create(*hist, path, DBOptions(key));
4172

4173
        TransactionRef rt = sg->start_read() LangBindHelper::promote_to_write(sg);
4174
        Group& group = const_cast<Group&>(rt.get_group());
4175
        TableRef t = rt->get_table(0);
4176

4177
        for (size_t i = 0; i < num_rows; ++i) {
4178
            const size_t payload_length = i % 10 == 0 ? payload_length_large : payload_length_small;
4179
            const char payload_char = 'a' + static_cast<char>((id + rep + i) % 26);
4180
            std::string std_payload(payload_length, payload_char);
4181
            StringData payload(std_payload);
4182

4183
            t->set_int(0, i, payload.size());
4184
            t->set_string(1, i, StringData(std_payload.c_str(), 1));
4185
            t->set_string(2, i, payload);
4186
        }
4187
        LangBindHelper::commit_and_continue_as_read(sg);
4188
    }
4189
}
4190

4191
void do_read_verify(std::string path)
4192
{
4193
    Random random(random_int<unsigned long>()); // Seed from slow global generator
4194
    const char* key = crypt_key(true);
4195
    while (true) {
4196
        std::unique_ptr<Replication> hist(make_in_realm_history());
4197
        DBRef sg = DB::create(*hist, path, DBOptions(key));
4198
        TransactionRef rt =
4199
            sg->start_read() if (rt.get_version() <= 2) continue; // let the writers make some initial data
4200
        Group& group = const_cast<Group&>(rt.get_group());
4201
        ConstTableRef t = rt->get_table(0);
4202
        size_t num_rows = t->size();
4203
        for (size_t r = 0; r < num_rows; ++r) {
4204
            int64_t num_chars = t->get_int(0, r);
4205
            StringData c = t->get_string(1, r);
4206
            if (c == "stop reading") {
4207
                return;
4208
            }
4209
            else {
4210
                REALM_ASSERT_EX(c.size() == 1, c.size());
4211
            }
4212
            REALM_ASSERT_EX(t->get_name() == StringData("class_Table_Emulation_Name"), t->get_name().data());
4213
            REALM_ASSERT_EX(t->get_column_name(0) == StringData("count"), t->get_column_name(0).data());
4214
            REALM_ASSERT_EX(t->get_column_name(1) == StringData("char"), t->get_column_name(1).data());
4215
            REALM_ASSERT_EX(t->get_column_name(2) == StringData("payload"), t->get_column_name(2).data());
4216
            std::string std_validator(static_cast<unsigned int>(num_chars), c[0]);
4217
            StringData validator(std_validator);
4218
            StringData s = t->get_string(2, r);
4219
            REALM_ASSERT_EX(s.size() == validator.size(), r, s.size(), validator.size());
4220
            for (size_t i = 0; i < s.size(); ++i) {
4221
                REALM_ASSERT_EX(s[i] == validator[i], r, i, s[i], validator[i]);
4222
            }
4223
            REALM_ASSERT_EX(s == validator, r, s.size(), validator.size());
4224
        }
4225
    }
4226
}
4227

4228
} // end anonymous namespace
4229

4230

4231
// The following test is long running to try to catch race conditions
4232
// in with many reader writer threads on an encrypted realm and it is
4233
// not suited to automated testing.
4234
TEST_IF(Thread_AsynchronousIODataConsistency, false)
4235
{
4236
    SHARED_GROUP_TEST_PATH(path);
4237
    const int num_writer_threads = 2;
4238
    const int num_reader_threads = 2;
4239
    const int num_rows = 200; // 2 + REALM_MAX_BPNODE_SIZE;
4240
    const char* key = crypt_key(true);
4241
    std::unique_ptr<Replication> hist(make_in_realm_history());
4242
    DBRef sg = DB::create(*hist, path, DBOptions(key));
4243
    {
4244
        WriteTransaction wt(sg);
4245
        Group& group = wt.get_group();
4246
        TableRef t = rt->add_table("class_Table_Emulation_Name");
4247
        // add a column for each thread to write to
4248
        t->add_column(type_Int, "count", true);
4249
        t->add_column(type_String, "char", true);
4250
        t->add_column(type_String, "payload", true);
4251
        t->add_empty_row(num_rows);
4252
        wt.commit();
4253
    }
4254

4255
    Thread writer_threads[num_writer_threads];
4256
    for (int i = 0; i < num_writer_threads; ++i) {
4257
        writer_threads[i].start(std::bind(do_write_work, std::string(path), i, num_rows));
4258
    }
4259
    Thread reader_threads[num_reader_threads];
4260
    for (int i = 0; i < num_reader_threads; ++i) {
4261
        reader_threads[i].start(std::bind(do_read_verify, std::string(path)));
4262
    }
4263
    for (int i = 0; i < num_writer_threads; ++i) {
4264
        writer_threads[i].join();
4265
    }
4266

4267
    {
4268
        WriteTransaction wt(sg);
4269
        Group& group = wt.get_group();
4270
        TableRef t = rt->get_table("class_Table_Emulation_Name");
4271
        t->set_string(1, 0, "stop reading");
4272
        wt.commit();
4273
    }
4274

4275
    for (int i = 0; i < num_reader_threads; ++i) {
4276
        reader_threads[i].join();
4277
    }
4278
}
4279
#endif
4280

4281

4282
TEST(LangBindHelper_HandoverTableRef)
4283
{
2✔
4284
    SHARED_GROUP_TEST_PATH(path);
2✔
4285
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4286
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4287
    TransactionRef reader;
2✔
4288
    TableRef table;
2✔
4289
    {
2✔
4290
        auto writer = sg->start_write();
2✔
4291
        TableRef table1 = writer->add_table("table1");
2✔
4292
        writer->commit_and_continue_as_read();
2✔
4293
        auto vid = writer->get_version_of_current_transaction();
2✔
4294
        reader = sg->start_read(vid);
2✔
4295
        table = reader->import_copy_of(table1);
2✔
4296
    }
2✔
4297
    CHECK(bool(table));
2✔
4298
    CHECK(table->size() == 0);
2✔
4299
}
2✔
4300

4301
TEST(LangBindHelper_HandoverLinkView)
4302
{
2✔
4303
    SHARED_GROUP_TEST_PATH(path);
2✔
4304
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4305
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4306
    TransactionRef reader;
2✔
4307
    ColKey col1;
2✔
4308

1✔
4309
    auto writer = sg->start_write();
2✔
4310

1✔
4311
    TableRef table1 = writer->add_table("table1");
2✔
4312
    TableRef table2 = writer->add_table("table2");
2✔
4313

1✔
4314
    // add some more columns to table1 and table2
1✔
4315
    col1 = table1->add_column(type_Int, "col1");
2✔
4316
    table1->add_column(type_String, "str1");
2✔
4317

1✔
4318
    // add some rows
1✔
4319
    auto to1 = table1->create_object().set_all(300, "delta");
2✔
4320
    auto to2 = table1->create_object().set_all(100, "alfa");
2✔
4321
    auto to3 = table1->create_object().set_all(200, "beta");
2✔
4322

1✔
4323
    ColKey col_link2 = table2->add_column_list(*table1, "linklist");
2✔
4324

1✔
4325
    auto o1 = table2->create_object();
2✔
4326
    table2->create_object();
2✔
4327
    LnkLstPtr lvr = o1.get_linklist_ptr(col_link2);
2✔
4328
    lvr->clear();
2✔
4329
    lvr->add(to1.get_key());
2✔
4330
    lvr->add(to2.get_key());
2✔
4331
    lvr->add(to3.get_key());
2✔
4332
    writer->commit_and_continue_as_read();
2✔
4333
    reader = writer->duplicate();
2✔
4334
    auto ll = reader->import_copy_of(lvr);
2✔
4335
    {
2✔
4336
        // validate inside reader transaction
1✔
4337
        // Return all rows of table1 (the linked-to-table) that match the criteria and is in the LinkList
1✔
4338

1✔
4339
        // q.m_table = table1
1✔
4340
        // q.m_view = lvr
1✔
4341
        TableRef table1b = reader->get_table("table1");
2✔
4342
        Query q = table1b->where(*ll).and_query(table1b->column<Int>(col1) > 100);
2✔
4343

1✔
4344
        // tv.m_table == table1
1✔
4345
        TableView tv = q.find_all(); // tv = { 0, 2 }
2✔
4346

1✔
4347

1✔
4348
        CHECK_EQUAL(2, tv.size());
2✔
4349
        CHECK_EQUAL(to1.get_key(), tv.get_key(0));
2✔
4350
        CHECK_EQUAL(to3.get_key(), tv.get_key(1));
2✔
4351
    }
2✔
4352
    {
2✔
4353
        // Change table1 and verify that the change does not propagate through the handed-over linkview
1✔
4354
        writer->promote_to_write();
2✔
4355
        to1.set<int64_t>(col1, 50);
2✔
4356
        writer->commit_and_continue_as_read();
2✔
4357
    }
2✔
4358
    {
2✔
4359
        TableRef table1b = reader->get_table("table1");
2✔
4360
        Query q = table1b->where(*ll).and_query(table1b->column<Int>(col1) > 100);
2✔
4361

1✔
4362
        // tv.m_table == table1
1✔
4363
        TableView tv = q.find_all(); // tv = { 0, 2 }
2✔
4364

1✔
4365

1✔
4366
        CHECK_EQUAL(2, tv.size());
2✔
4367
        CHECK_EQUAL(to1.get_key(), tv.get_key(0));
2✔
4368
        CHECK_EQUAL(to3.get_key(), tv.get_key(1));
2✔
4369
    }
2✔
4370
}
2✔
4371

4372
TEST(LangBindHelper_HandoverDistinctView)
4373
{
2✔
4374
    SHARED_GROUP_TEST_PATH(path);
2✔
4375
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4376
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4377
    TransactionRef reader;
2✔
4378
    std::unique_ptr<TableView> tv2;
2✔
4379
    Obj obj2b;
2✔
4380
    {
2✔
4381
        {
2✔
4382
            TableView tv1;
2✔
4383
            auto writer = sg->start_write();
2✔
4384
            TableRef table = writer->add_table("table2");
2✔
4385
            auto col = table->add_column(type_Int, "first");
2✔
4386
            auto obj1 = table->create_object().set_all(100);
2✔
4387
            table->create_object().set_all(100);
2✔
4388

1✔
4389
            writer->commit_and_continue_as_read();
2✔
4390
            tv1 = table->where().find_all();
2✔
4391
            tv1.distinct(col);
2✔
4392
            CHECK(tv1.size() == 1);
2✔
4393
            CHECK(tv1.get_key(0) == obj1.get_key());
2✔
4394
            CHECK(tv1.is_attached());
2✔
4395

1✔
4396
            reader = writer->duplicate();
2✔
4397
            tv2 = reader->import_copy_of(tv1, PayloadPolicy::Copy);
2✔
4398
            obj2b = reader->import_copy_of(obj1);
2✔
4399
            CHECK(tv1.is_attached());
2✔
4400
        }
2✔
4401
        {
2✔
4402
            // importing side: working in the context of "reader"
1✔
4403
            CHECK(tv2->is_in_sync());
2✔
4404
            CHECK(tv2->is_attached());
2✔
4405

1✔
4406
            CHECK_EQUAL(tv2->size(), 1);
2✔
4407
            CHECK_EQUAL(tv2->get_key(0), obj2b.get_key());
2✔
4408

1✔
4409
            // distinct property must remain through handover such that second row is kept being omitted
1✔
4410
            // after sync_if_needed()
1✔
4411
            tv2->sync_if_needed();
2✔
4412
            CHECK_EQUAL(tv2->size(), 1);
2✔
4413
            CHECK_EQUAL(tv2->get_key(0), obj2b.get_key());
2✔
4414
        }
2✔
4415
    }
2✔
4416
}
2✔
4417

4418

4419
TEST(LangBindHelper_HandoverWithReverseDependency)
4420
{
2✔
4421
    SHARED_GROUP_TEST_PATH(path);
2✔
4422
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4423
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4424
    auto trans = sg->start_read();
2✔
4425
    {
2✔
4426
        // Untyped interface
1✔
4427
        TableView tv1;
2✔
4428
        TableView tv2;
2✔
4429
        ColKey ck;
2✔
4430
        {
2✔
4431
            trans->promote_to_write();
2✔
4432
            TableRef table = trans->add_table("table2");
2✔
4433
            ck = table->add_column(type_Int, "first");
2✔
4434
            for (int i = 0; i < 100; ++i) {
202✔
4435
                table->create_object().set_all(i);
200✔
4436
            }
200✔
4437
            trans->commit_and_continue_as_read();
2✔
4438
            tv1 = table->where().find_all();
2✔
4439
            tv2 = table->where(&tv1).find_all();
2✔
4440
            CHECK(tv1.is_attached());
2✔
4441
            CHECK(tv2.is_attached());
2✔
4442
            CHECK_EQUAL(100, tv1.size());
2✔
4443
            for (int i = 0; i < 100; ++i)
202✔
4444
                CHECK_EQUAL(i, tv1.get_object(i).get<int64_t>(ck));
200✔
4445
            CHECK_EQUAL(100, tv2.size());
2✔
4446
            for (int i = 0; i < 100; ++i)
202✔
4447
                CHECK_EQUAL(i, tv1.get_object(i).get<int64_t>(ck));
200✔
4448
            auto dummy_trans = trans->duplicate();
2✔
4449
            auto dummy_tv = dummy_trans->import_copy_of(tv1, PayloadPolicy::Copy);
2✔
4450
            CHECK(tv1.is_attached());
2✔
4451
            CHECK(tv2.is_attached());
2✔
4452
        }
2✔
4453
    }
2✔
4454
}
2✔
4455

4456
TEST(LangBindHelper_HandoverTableViewFromBacklink)
4457
{
2✔
4458
    SHARED_GROUP_TEST_PATH(path);
2✔
4459
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4460
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4461
    auto group_w = sg->start_write();
2✔
4462

1✔
4463
    TableRef source = group_w->add_table("source");
2✔
4464
    source->add_column(type_Int, "int");
2✔
4465

1✔
4466
    TableRef links = group_w->add_table("links");
2✔
4467
    ColKey col = links->add_column(*source, "link");
2✔
4468

1✔
4469
    std::vector<ObjKey> dummies;
2✔
4470
    source->create_objects(100, dummies);
2✔
4471
    links->create_objects(100, dummies);
2✔
4472
    auto source_it = source->begin();
2✔
4473
    auto links_it = links->begin();
2✔
4474
    for (int i = 0; i < 100; ++i) {
202✔
4475
        auto obj = source_it->set_all(i);
200✔
4476
        links_it->set(col, obj.get_key());
200✔
4477
        ++source_it;
200✔
4478
        ++links_it;
200✔
4479
    }
200✔
4480
    group_w->commit_and_continue_as_read();
2✔
4481

1✔
4482
    for (int i = 0; i < 100; ++i) {
202✔
4483
        TableView tv = source->get_object(i).get_backlink_view(links, col);
200✔
4484
        CHECK(tv.is_attached());
200✔
4485
        CHECK_EQUAL(1, tv.size());
200✔
4486
        ObjKey o_key = source->get_object(i).get_key();
200✔
4487
        CHECK_EQUAL(o_key, tv.get_key(0));
200✔
4488
        auto group = group_w->duplicate();
200✔
4489
        auto tv2 = group->import_copy_of(tv, PayloadPolicy::Copy);
200✔
4490
        CHECK(tv.is_attached());
200✔
4491
        CHECK(tv2->is_attached());
200✔
4492
        CHECK_EQUAL(1, tv2->size());
200✔
4493
        CHECK_EQUAL(o_key, tv2->get_key(0));
200✔
4494
    }
200✔
4495
}
2✔
4496

4497
// Verify that handing over an out-of-sync TableView that represents backlinks
4498
// to a deleted row results in a TableView that can be brought back into sync.
4499
TEST(LangBindHelper_HandoverOutOfSyncTableViewFromBacklinksToDeletedRow)
4500
{
2✔
4501
    SHARED_GROUP_TEST_PATH(path);
2✔
4502
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4503
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4504
    auto group_w = sg->start_write();
2✔
4505

1✔
4506
    TableRef target = group_w->add_table("target");
2✔
4507
    target->add_column(type_Int, "int");
2✔
4508

1✔
4509
    TableRef links = group_w->add_table("links");
2✔
4510
    auto col = links->add_column(*target, "link");
2✔
4511

1✔
4512
    auto obj_t = target->create_object().set_all(0);
2✔
4513

1✔
4514
    links->create_object().set_all(obj_t.get_key());
2✔
4515

1✔
4516
    TableView tv = obj_t.get_backlink_view(links, col);
2✔
4517
    CHECK_EQUAL(true, tv.is_attached());
2✔
4518
    CHECK_EQUAL(true, tv.is_in_sync());
2✔
4519
    CHECK_EQUAL(false, tv.depends_on_deleted_object());
2✔
4520
    CHECK_EQUAL(1, tv.size());
2✔
4521

1✔
4522
    // Bring the view out of sync, and have it depend on a deleted row.
1✔
4523
    target->remove_object(obj_t.get_key());
2✔
4524
    CHECK_EQUAL(true, tv.is_attached());
2✔
4525
    CHECK_EQUAL(false, tv.is_in_sync());
2✔
4526
    CHECK_EQUAL(true, tv.depends_on_deleted_object());
2✔
4527
    CHECK_EQUAL(1, tv.size());
2✔
4528
    tv.sync_if_needed();
2✔
4529
    CHECK_EQUAL(0, tv.size());
2✔
4530
    group_w->commit_and_continue_as_read();
2✔
4531
    auto group = group_w->duplicate();
2✔
4532
    auto tv2 = group->import_copy_of(tv, PayloadPolicy::Copy);
2✔
4533
    CHECK_EQUAL(true, tv2->depends_on_deleted_object());
2✔
4534
    CHECK_EQUAL(0, tv2->size());
2✔
4535
}
2✔
4536

4537
// Test that we can handover a query involving links, and that after the
4538
// handover export, the handover is completely decoupled from later changes
4539
// done on accessors belonging to the exporting shared group
4540
TEST(LangBindHelper_HandoverWithLinkQueries)
4541
{
2✔
4542
    SHARED_GROUP_TEST_PATH(path);
2✔
4543
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4544
    DBRef db = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4545
    auto group_w = db->start_write();
2✔
4546
    // First setup data so that we can do a query on links
1✔
4547
    TableRef table1 = group_w->add_table("table1");
2✔
4548
    TableRef table2 = group_w->add_table("table2");
2✔
4549
    // add some more columns to table1 and table2
1✔
4550
    table1->add_column(type_Int, "col1");
2✔
4551
    table1->add_column(type_String, "str1");
2✔
4552

1✔
4553
    table2->add_column(type_Int, "col1");
2✔
4554
    auto col_str = table2->add_column(type_String, "str2");
2✔
4555

1✔
4556
    // add some rows
1✔
4557
    auto o10 = table1->create_object().set_all(100, "foo");
2✔
4558
    auto o11 = table1->create_object().set_all(200, "!");
2✔
4559
    table1->create_object().set_all(300, "bar");
2✔
4560
    table2->create_object().set_all(400, "hello");
2✔
4561
    auto o21 = table2->create_object().set_all(500, "world");
2✔
4562
    auto o22 = table2->create_object().set_all(600, "!");
2✔
4563

1✔
4564
    ColKey col_link2 = table1->add_column_list(*table2, "link");
2✔
4565

1✔
4566
    // set some links
1✔
4567
    auto links1 = o10.get_linklist(col_link2);
2✔
4568
    CHECK(links1.is_attached());
2✔
4569
    links1.add(o21.get_key());
2✔
4570

1✔
4571
    auto links2 = o11.get_linklist(col_link2);
2✔
4572
    CHECK(links2.is_attached());
2✔
4573
    links2.add(o21.get_key());
2✔
4574
    links2.add(o22.get_key());
2✔
4575
    group_w->commit_and_continue_as_read();
2✔
4576

1✔
4577
    // Do a query (which will have zero results) and export it twice.
1✔
4578
    // To test separation, we'll later modify state at the exporting side,
1✔
4579
    // and verify that the two different imports still get identical results
1✔
4580
    realm::Query query = table1->link(col_link2).column<String>(col_str) == "nabil";
2✔
4581
    realm::TableView tv4 = query.find_all();
2✔
4582

1✔
4583
    auto rec1 = group_w->duplicate();
2✔
4584
    auto q1 = rec1->import_copy_of(query, PayloadPolicy::Copy);
2✔
4585
    auto rec2 = group_w->duplicate();
2✔
4586
    auto q2 = rec2->import_copy_of(query, PayloadPolicy::Copy);
2✔
4587

1✔
4588
    {
2✔
4589
        realm::TableView tv = q1->find_all();
2✔
4590
        CHECK_EQUAL(0, tv.size());
2✔
4591
    }
2✔
4592

1✔
4593
    // On the exporting side, change the data such that the query will now have
1✔
4594
    // non-zero results if evaluated in that context.
1✔
4595
    group_w->promote_to_write();
2✔
4596
    auto o23 = table2->create_object().set_all(700, "nabil");
2✔
4597
    links1.add(o23.get_key());
2✔
4598
    group_w->commit_and_continue_as_read();
2✔
4599
    CHECK_EQUAL(1, query.count());
2✔
4600
    {
2✔
4601
        // Import query and evaluate in the old context. This should *not* be
1✔
4602
        // affected by the change done above on the exporting side.
1✔
4603
        realm::TableView tv2 = q2->find_all();
2✔
4604
        CHECK_EQUAL(0, tv2.size());
2✔
4605
    }
2✔
4606
}
2✔
4607

4608

4609
TEST(LangBindHelper_HandoverQueryLinksTo)
4610
{
2✔
4611
    SHARED_GROUP_TEST_PATH(path);
2✔
4612
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4613
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4614

1✔
4615
    TransactionRef reader;
2✔
4616
    std::unique_ptr<Query> query;
2✔
4617
    std::unique_ptr<Query> queryOr;
2✔
4618
    std::unique_ptr<Query> queryAnd;
2✔
4619
    std::unique_ptr<Query> queryNot;
2✔
4620
    std::unique_ptr<Query> queryAndAndOr;
2✔
4621
    std::unique_ptr<Query> queryWithExpression;
2✔
4622
    std::unique_ptr<Query> queryLinksToDetached;
2✔
4623
    {
2✔
4624
        auto group_w = sg->start_write();
2✔
4625
        TableRef source = group_w->add_table("source");
2✔
4626
        TableRef target = group_w->add_table("target");
2✔
4627

1✔
4628
        ColKey col_link = source->add_column(*target, "link");
2✔
4629
        ColKey col_name = target->add_column(type_String, "name");
2✔
4630

1✔
4631
        std::vector<ObjKey> keys;
2✔
4632
        target->create_objects(4, keys);
2✔
4633
        target->get_object(0).set(col_name, "A");
2✔
4634
        target->get_object(1).set(col_name, "B");
2✔
4635
        target->get_object(2).set(col_name, "C");
2✔
4636
        target->get_object(3).set(col_name, "D");
2✔
4637

1✔
4638
        source->create_object().set_all(keys[0]);
2✔
4639
        source->create_object().set_all(keys[1]);
2✔
4640
        source->create_object().set_all(keys[2]);
2✔
4641

1✔
4642
        Obj detached_row = target->get_object(3);
2✔
4643
        target->remove_object(detached_row.get_key());
2✔
4644

1✔
4645
        group_w->commit_and_continue_as_read();
2✔
4646

1✔
4647
        Query _query = source->column<Link>(col_link) == target->get_object(0);
2✔
4648
        Query _queryOr = source->column<Link>(col_link) == target->get_object(0) ||
2✔
4649
                         source->column<Link>(col_link) == target->get_object(1);
2✔
4650
        Query _queryAnd = source->column<Link>(col_link) == target->get_object(0) &&
2✔
4651
                          source->column<Link>(col_link) == target->get_object(0);
2✔
4652
        Query _queryNot = !(source->column<Link>(col_link) == target->get_object(0)) &&
2✔
4653
                          source->column<Link>(col_link) == target->get_object(1);
2✔
4654
        Query _queryAndAndOr = source->where().group().and_query(_queryOr).end_group().and_query(_queryAnd);
2✔
4655
        Query _queryWithExpression = source->column<Link>(col_link).is_not_null() && _query;
2✔
4656
        Query _queryLinksToDetached = source->where().links_to(col_link, detached_row.get_key());
2✔
4657

1✔
4658
        // handover:
1✔
4659
        reader = group_w->duplicate();
2✔
4660
        query = reader->import_copy_of(_query, PayloadPolicy::Copy);
2✔
4661
        queryOr = reader->import_copy_of(_queryOr, PayloadPolicy::Copy);
2✔
4662
        queryAnd = reader->import_copy_of(_queryAnd, PayloadPolicy::Copy);
2✔
4663
        queryNot = reader->import_copy_of(_queryNot, PayloadPolicy::Copy);
2✔
4664
        queryAndAndOr = reader->import_copy_of(_queryAndAndOr, PayloadPolicy::Copy);
2✔
4665
        queryWithExpression = reader->import_copy_of(_queryWithExpression, PayloadPolicy::Copy);
2✔
4666
        queryLinksToDetached = reader->import_copy_of(_queryLinksToDetached, PayloadPolicy::Copy);
2✔
4667

1✔
4668
        CHECK_EQUAL(1, _query.count());
2✔
4669
        CHECK_EQUAL(2, _queryOr.count());
2✔
4670
        CHECK_EQUAL(1, _queryAnd.count());
2✔
4671
        CHECK_EQUAL(1, _queryNot.count());
2✔
4672
        CHECK_EQUAL(1, _queryAndAndOr.count());
2✔
4673
        CHECK_EQUAL(1, _queryWithExpression.count());
2✔
4674
        CHECK_EQUAL(0, _queryLinksToDetached.count());
2✔
4675
    }
2✔
4676
    {
2✔
4677
        CHECK_EQUAL(1, query->count());
2✔
4678
        CHECK_EQUAL(2, queryOr->count());
2✔
4679
        CHECK_EQUAL(1, queryAnd->count());
2✔
4680
        CHECK_EQUAL(1, queryNot->count());
2✔
4681
        CHECK_EQUAL(1, queryAndAndOr->count());
2✔
4682
        CHECK_EQUAL(1, queryWithExpression->count());
2✔
4683
        CHECK_EQUAL(0, queryLinksToDetached->count());
2✔
4684

1✔
4685

1✔
4686
        // Remove the linked-to row.
1✔
4687
        {
2✔
4688
            auto group_w = sg->start_write();
2✔
4689
            TableRef target = group_w->get_table("target");
2✔
4690
            target->remove_object(target->begin()->get_key());
2✔
4691
            group_w->commit();
2✔
4692
        }
2✔
4693

1✔
4694
        // Verify that the queries against the read-only shared group gives the same results.
1✔
4695
        CHECK_EQUAL(1, query->count());
2✔
4696
        CHECK_EQUAL(2, queryOr->count());
2✔
4697
        CHECK_EQUAL(1, queryAnd->count());
2✔
4698
        CHECK_EQUAL(1, queryNot->count());
2✔
4699
        CHECK_EQUAL(1, queryAndAndOr->count());
2✔
4700
        CHECK_EQUAL(1, queryWithExpression->count());
2✔
4701
        CHECK_EQUAL(0, queryLinksToDetached->count());
2✔
4702
    }
2✔
4703
}
2✔
4704

4705

4706
TEST(LangBindHelper_HandoverQuerySubQuery)
4707
{
2✔
4708
    SHARED_GROUP_TEST_PATH(path);
2✔
4709
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4710
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4711

1✔
4712
    TransactionRef reader;
2✔
4713
    std::unique_ptr<Query> query;
2✔
4714
    {
2✔
4715
        auto group_w = sg->start_write();
2✔
4716

1✔
4717
        TableRef source = group_w->add_table("source");
2✔
4718
        TableRef target = group_w->add_table("target");
2✔
4719

1✔
4720
        ColKey col_link = source->add_column(*target, "link");
2✔
4721
        ColKey col_name = target->add_column(type_String, "name");
2✔
4722

1✔
4723
        std::vector<ObjKey> keys;
2✔
4724
        target->create_objects(3, keys);
2✔
4725
        target->get_object(keys[0]).set(col_name, "A");
2✔
4726
        target->get_object(keys[1]).set(col_name, "B");
2✔
4727
        target->get_object(keys[2]).set(col_name, "C");
2✔
4728

1✔
4729
        source->create_object().set_all(keys[0]);
2✔
4730
        source->create_object().set_all(keys[1]);
2✔
4731
        source->create_object().set_all(keys[2]);
2✔
4732

1✔
4733
        group_w->commit_and_continue_as_read();
2✔
4734

1✔
4735
        realm::Query query_2 = source->column<Link>(col_link, target->column<String>(col_name) == "C").count() == 1;
2✔
4736
        reader = group_w->duplicate();
2✔
4737
        query = reader->import_copy_of(query_2, PayloadPolicy::Copy);
2✔
4738
    }
2✔
4739

1✔
4740
    CHECK_EQUAL(1, query->count());
2✔
4741

1✔
4742
    // Remove the linked-to row.
1✔
4743
    {
2✔
4744
        auto group_w = sg->start_write();
2✔
4745

1✔
4746
        TableRef target = group_w->get_table("target");
2✔
4747
        target->clear();
2✔
4748
        group_w->commit_and_continue_as_read();
2✔
4749
    }
2✔
4750

1✔
4751
    // Verify that the queries against the read-only shared group gives the same results.
1✔
4752
    CHECK_EQUAL(1, query->count());
2✔
4753
}
2✔
4754

4755
TEST(LangBindHelper_VersionControl)
4756
{
2✔
4757
    Random random(random_int<unsigned long>());
2✔
4758

1✔
4759
    const int num_versions = 10;
2✔
4760
    const int num_random_tests = 100;
2✔
4761
    DB::VersionID versions[num_versions];
2✔
4762
    std::vector<TransactionRef> trs;
2✔
4763
    SHARED_GROUP_TEST_PATH(path);
2✔
4764
    {
2✔
4765
        // Create a new shared db
1✔
4766
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4767
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4768
        // first create 'num_version' versions
1✔
4769
        ColKey col;
2✔
4770
        auto reader = sg->start_read();
2✔
4771
        {
2✔
4772
            WriteTransaction wt(sg);
2✔
4773
            col = wt.get_or_add_table("test")->add_column(type_Int, "a");
2✔
4774
            wt.commit();
2✔
4775
        }
2✔
4776
        for (int i = 0; i < num_versions; ++i) {
22✔
4777
            {
20✔
4778
                WriteTransaction wt(sg);
20✔
4779
                auto t = wt.get_table("test");
20✔
4780
                t->create_object().set_all(i);
20✔
4781
                wt.commit();
20✔
4782
            }
20✔
4783
            {
20✔
4784
                auto rt = sg->start_read();
20✔
4785
                trs.push_back(rt->duplicate());
20✔
4786
                versions[i] = rt->get_version_of_current_transaction();
20✔
4787
            }
20✔
4788
        }
20✔
4789

1✔
4790
        // do steps of increasing size from the first version to the last,
1✔
4791
        // including a "step on the spot" (from version 0 to 0)
1✔
4792
        {
2✔
4793
            for (int k = 0; k < num_versions; ++k) {
22✔
4794
                // std::cerr << "Advancing from initial version to version " << k << std::endl;
10✔
4795
                auto g = sg->start_read(versions[0]);
20✔
4796
                auto t = g->get_table("test");
20✔
4797
                CHECK(versions[k] >= versions[0]);
20✔
4798
                g->verify();
20✔
4799
                g->advance_read(versions[k]);
20✔
4800
                g->verify();
20✔
4801
                auto o = *(t->begin() + k);
20✔
4802
                CHECK_EQUAL(k, o.get<int64_t>(col));
20✔
4803
            }
20✔
4804
        }
2✔
4805

1✔
4806
        // step through the versions backward:
1✔
4807
        for (int i = num_versions - 1; i >= 0; --i) {
22✔
4808
            // std::cerr << "Jumping directly to version " << i << std::endl;
10✔
4809

10✔
4810
            auto g = sg->start_read(versions[i]);
20✔
4811
            g->verify();
20✔
4812
            auto t = g->get_table("test");
20✔
4813
            auto o = *(t->begin() + i);
20✔
4814
            CHECK_EQUAL(i, o.get<int64_t>(col));
20✔
4815
        }
20✔
4816

1✔
4817
        // then advance through the versions going forward
1✔
4818
        {
2✔
4819
            auto g = sg->start_read(versions[0]);
2✔
4820
            g->verify();
2✔
4821
            auto t = g->get_table("test");
2✔
4822
            for (int k = 0; k < num_versions; ++k) {
22✔
4823
                // std::cerr << "Advancing to version " << k << std::endl;
10✔
4824
                CHECK(k == 0 || versions[k] >= versions[k - 1]);
20✔
4825

10✔
4826
                g->advance_read(versions[k]);
20✔
4827
                g->verify();
20✔
4828
                auto o = *(t->begin() + k);
20✔
4829
                CHECK_EQUAL(k, o.get<int64_t>(col));
20✔
4830
            }
20✔
4831
        }
2✔
4832
        // sync to a randomly selected version - use advance_read when going
1✔
4833
        // forward in time, but begin_read when going back in time
1✔
4834
        int old_version = 0;
2✔
4835
        auto g = sg->start_read(versions[old_version]);
2✔
4836
        auto t = g->get_table("test");
2✔
4837
        for (int k = num_random_tests; k; --k) {
202✔
4838
            int new_version = random.draw_int_mod(num_versions);
200✔
4839
            // std::cerr << "Random jump: version " << old_version << " -> " << new_version << std::endl;
100✔
4840
            if (new_version < old_version) {
200✔
4841
                CHECK(versions[new_version] < versions[old_version]);
88✔
4842
                g->end_read();
88✔
4843
                g = sg->start_read(versions[new_version]);
88✔
4844
                g->verify();
88✔
4845
                t = g->get_table("test");
88✔
4846
                auto o = *(t->begin() + new_version);
88✔
4847
                CHECK_EQUAL(new_version, o.get<int64_t>(col));
88✔
4848
            }
88✔
4849
            else {
112✔
4850
                CHECK(versions[new_version] >= versions[old_version]);
112✔
4851
                g->verify();
112✔
4852
                g->advance_read(versions[new_version]);
112✔
4853
                g->verify();
112✔
4854
                auto o = *(t->begin() + new_version);
112✔
4855
                CHECK_EQUAL(new_version, o.get<int64_t>(col));
112✔
4856
            }
112✔
4857
            old_version = new_version;
200✔
4858
        }
200✔
4859
        trs.clear();
2✔
4860
        g->end_read();
2✔
4861
        // release the first readlock and commit something to force a cleanup
1✔
4862
        // we need to commit twice, because cleanup is done before the actual
1✔
4863
        // commit, so during the first commit, the last of the previous versions
1✔
4864
        // will still be kept. To get rid of it, we must commit once more.
1✔
4865
        reader->end_read();
2✔
4866
        g = sg->start_write();
2✔
4867
        g->commit();
2✔
4868
        g = sg->start_write();
2✔
4869
        g->commit();
2✔
4870

1✔
4871
        // Validate that all the versions are now unreachable
1✔
4872
        for (int i = 0; i < num_versions; ++i)
22✔
4873
            CHECK_THROW(sg->start_read(versions[i]), DB::BadVersion);
20✔
4874
    }
2✔
4875
}
2✔
4876

4877
TEST(LangBindHelper_RollbackToInitialState1)
4878
{
2✔
4879
    SHARED_GROUP_TEST_PATH(path);
2✔
4880
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
4881
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
4882
    auto trans = sg_w->start_read();
2✔
4883
    trans->promote_to_write();
2✔
4884
    trans->rollback_and_continue_as_read();
2✔
4885
}
2✔
4886

4887

4888
TEST(LangBindHelper_RollbackToInitialState2)
4889
{
2✔
4890
    SHARED_GROUP_TEST_PATH(path);
2✔
4891
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
4892
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
4893
    auto trans = sg_w->start_write();
2✔
4894
    trans->rollback();
2✔
4895
}
2✔
4896

4897
// non-concurrent because we test the filesystem which may
4898
// be used by other tests at the same time otherwise
4899
NONCONCURRENT_TEST(LangBindHelper_Compact)
4900
{
2✔
4901
    SHARED_GROUP_TEST_PATH(path);
2✔
4902
    size_t N = 100;
2✔
4903
    std::string dir_path = File::parent_dir(path);
2✔
4904
    dir_path = dir_path.empty() ? "." : dir_path;
2✔
4905
    auto dir_has_tmp_compaction = [&dir_path]() -> size_t {
6✔
4906
        DirScanner dir(dir_path);
6✔
4907
        std::string name;
6✔
4908
        while (dir.next(name)) {
288✔
4909
            if (name.find("tmp_compaction_space") != std::string::npos) {
282✔
4910
                return true;
×
4911
            }
×
4912
        }
282✔
4913
        return false;
6✔
4914
    };
6✔
4915

1✔
4916
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4917
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4918
    {
2✔
4919
        WriteTransaction w(sg);
2✔
4920
        TableRef table = w.get_or_add_table("test");
2✔
4921
        table->add_column(type_Int, "int");
2✔
4922
        for (size_t i = 0; i < N; ++i) {
202✔
4923
            table->create_object().set_all(static_cast<signed>(i));
200✔
4924
        }
200✔
4925
        w.commit();
2✔
4926
    }
2✔
4927
    {
2✔
4928
        ReadTransaction r(sg);
2✔
4929
        ConstTableRef table = r.get_table("test");
2✔
4930
        CHECK_EQUAL(N, table->size());
2✔
4931
        CHECK(File::exists(dir_path));
2✔
4932
        CHECK(File::is_dir(dir_path));
2✔
4933
        CHECK(!dir_has_tmp_compaction());
2✔
4934
    }
2✔
4935
    {
2✔
4936
        CHECK_EQUAL(true, sg->compact());
2✔
4937
        CHECK(!dir_has_tmp_compaction());
2✔
4938
    }
2✔
4939
    {
2✔
4940
        ReadTransaction r(sg);
2✔
4941
        ConstTableRef table = r.get_table("test");
2✔
4942
        CHECK_EQUAL(N, table->size());
2✔
4943
    }
2✔
4944
    {
2✔
4945
        WriteTransaction w(sg);
2✔
4946
        TableRef table = w.get_or_add_table("test");
2✔
4947
        table->create_object().set_all(0);
2✔
4948
        w.commit();
2✔
4949
    }
2✔
4950
    {
2✔
4951
        CHECK_EQUAL(true, sg->compact());
2✔
4952
        CHECK(!dir_has_tmp_compaction());
2✔
4953
    }
2✔
4954
}
2✔
4955

4956
TEST(LangBindHelper_CompactLargeEncryptedFile)
4957
{
2✔
4958
    SHARED_GROUP_TEST_PATH(path);
2✔
4959

1✔
4960
    std::vector<char> data(realm::util::page_size());
2✔
4961
    const size_t N = 32;
2✔
4962

1✔
4963
    {
2✔
4964
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4965
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
4966
        WriteTransaction wt(sg);
2✔
4967
        TableRef table = wt.get_or_add_table("test");
2✔
4968
        table->add_column(type_String, "string");
2✔
4969
        for (size_t i = 0; i < N; ++i) {
66✔
4970
            table->create_object().set_all(StringData(data.data(), data.size()));
64✔
4971
        }
64✔
4972
        wt.commit();
2✔
4973

1✔
4974
        CHECK_EQUAL(true, sg->compact());
2✔
4975

1✔
4976
        sg->close();
2✔
4977
    }
2✔
4978

1✔
4979
    {
2✔
4980
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4981
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
4982
        ReadTransaction r(sg);
2✔
4983
        ConstTableRef table = r.get_table("test");
2✔
4984
        CHECK_EQUAL(N, table->size());
2✔
4985
    }
2✔
4986
}
2✔
4987

4988
TEST(LangBindHelper_CloseDBvsTransactions)
4989
{
2✔
4990
    SHARED_GROUP_TEST_PATH(path);
2✔
4991
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4992
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
4993
    auto tr0 = sg->start_read();
2✔
4994
    auto tr1 = sg->start_write();
2✔
4995
    CHECK(tr1->add_table("possible"));
2✔
4996
    // write transactions must be closed (one way or the other) before DB::close
1✔
4997
    CHECK_THROW(sg->close(), LogicError);
2✔
4998
    tr1->rollback();
2✔
4999
    // closing the DB explicitly while there are open read transactions will fail
1✔
5000
    CHECK_THROW(sg->close(), LogicError);
2✔
5001
    // unless we explicitly ask for it to succeed()
1✔
5002
    sg->close(true);
2✔
5003
    CHECK(!sg->is_attached());
2✔
5004
    CHECK(!tr0->is_attached());
2✔
5005
    CHECK(!tr1->is_attached());
2✔
5006
    CHECK_THROW(sg->start_read(), LogicError);
2✔
5007
}
2✔
5008

5009
TEST(LangBindHelper_TableViewAggregateAfterAdvanceRead)
5010
{
2✔
5011
    SHARED_GROUP_TEST_PATH(path);
2✔
5012

1✔
5013
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5014
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5015
    ColKey col;
2✔
5016
    {
2✔
5017
        WriteTransaction w(sg_w);
2✔
5018
        TableRef table = w.add_table("test");
2✔
5019
        col = table->add_column(type_Double, "double");
2✔
5020
        table->create_object().set_all(1234.0);
2✔
5021
        table->create_object().set_all(-5678.0);
2✔
5022
        table->create_object().set_all(1000.0);
2✔
5023
        w.commit();
2✔
5024
    }
2✔
5025

1✔
5026
    auto reader = sg_w->start_read();
2✔
5027
    auto table_r = reader->get_table("test");
2✔
5028

1✔
5029
    // Create a table view with all refs detached.
1✔
5030
    TableView view = table_r->where().find_all();
2✔
5031
    {
2✔
5032
        WriteTransaction w(sg_w);
2✔
5033
        w.get_table("test")->clear();
2✔
5034
        w.commit();
2✔
5035
    }
2✔
5036
    reader->advance_read();
2✔
5037

1✔
5038
    // Verify that an aggregate on the view with detached refs gives the expected result.
1✔
5039
    CHECK_EQUAL(false, view.is_in_sync());
2✔
5040
    ObjKey res;
2✔
5041
    CHECK(view.min(col, &res)->is_null());
2✔
5042
    CHECK_EQUAL(ObjKey(), res);
2✔
5043

1✔
5044
    // Sync the view to discard the detached refs.
1✔
5045
    view.sync_if_needed();
2✔
5046

1✔
5047
    // Verify that an aggregate on the view still gives the expected result.
1✔
5048
    res = ObjKey();
2✔
5049
    CHECK(view.min(col, &res)->is_null());
2✔
5050
    CHECK_EQUAL(ObjKey(), res);
2✔
5051
}
2✔
5052

5053
// Tests handover of a Query. Especially it tests if next-gen-syntax nodes are deep copied correctly by
5054
// executing an imported query multiple times in parallel
5055
TEST_IF(LangBindHelper_HandoverFuzzyTest, TEST_DURATION > 0)
5056
{
×
5057
    SHARED_GROUP_TEST_PATH(path);
×
5058

5059
    const size_t threads = 5;
×
5060

5061
    size_t numberOfOwner = 100;
×
5062
    size_t numberOfDogsPerOwner = 20;
×
5063

5064
    std::atomic<bool> end_signal(false);
×
5065
    std::unique_ptr<Replication> hist(make_in_realm_history());
×
5066
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
×
5067

5068
    std::vector<TransactionRef> vids;
×
5069
    std::vector<std::unique_ptr<Query>> qs;
×
5070
    std::mutex vector_mutex;
×
5071

5072
    ColKey c0, c1, c2, c3;
×
5073
    {
×
5074
        auto rt = sg->start_write();
×
5075

5076
        TableRef owner = rt->add_table("Owner");
×
5077
        TableRef dog = rt->add_table("Dog");
×
5078

5079
        c0 = owner->add_column(type_String, "name");
×
5080
        c1 = owner->add_column_list(*dog, "link");
×
5081

5082
        c2 = dog->add_column(type_String, "name");
×
5083
        c3 = dog->add_column(*owner, "link");
×
5084

5085
        for (size_t i = 0; i < numberOfOwner; i++) {
×
5086

5087
            auto o = owner->create_object();
×
5088
            std::string owner_str(std::string("owner") + to_string(i));
×
5089
            o.set<StringData>(c0, owner_str);
×
5090

5091
            for (size_t j = 0; j < numberOfDogsPerOwner; j++) {
×
5092
                auto o_d = dog->create_object();
×
5093
                std::string dog_str(std::string("dog") + to_string(i * numberOfOwner + j));
×
5094
                o_d.set<StringData>(c2, dog_str);
×
5095
                o_d.set(c3, o.get_key());
×
5096
                auto ll = o.get_linklist(c1);
×
5097
                ll.add(o_d.get_key());
×
5098
            }
×
5099
        }
×
5100
        rt->verify();
×
5101
        {
×
5102
            realm::Query query = dog->link(c3).column<String>(c0) == "owner" + to_string(rand() % numberOfOwner);
×
5103
            query.find_all(); // <-- fails
×
5104
        }
×
5105
        rt->commit();
×
5106
    }
×
5107

5108
    auto async = [&]() {
×
5109
        // Async thread
5110
        //************************************************************************************************
5111
        while (!end_signal) {
×
5112
            millisleep(10);
×
5113

5114
            vector_mutex.lock();
×
5115
            if (qs.size() > 0) {
×
5116

5117
                auto t = vids[0];
×
5118
                vids.erase(vids.begin());
×
5119
                auto q = std::move(qs[0]);
×
5120
                qs.erase(qs.begin());
×
5121
                vector_mutex.unlock();
×
5122

5123
                realm::TableView tv = q->find_all();
×
5124
            }
×
5125
            else {
×
5126
                vector_mutex.unlock();
×
5127
            }
×
5128
        }
×
5129
        //************************************************************************************************
5130
    };
×
5131

5132
    auto rt = sg->start_read();
×
5133
    // Create and export query
5134
    TableRef dog = rt->get_table("Dog");
×
5135

5136
    realm::Query query = dog->link(c3).column<String>(c0) == "owner" + to_string(rand() % numberOfOwner);
×
5137
    query.find_all(); // <-- fails
×
5138

5139
    Thread slaves[threads];
×
5140
    for (int i = 0; i != threads; ++i) {
×
5141
        slaves[i].start([=] {
×
5142
            async();
×
5143
        });
×
5144
    }
×
5145

5146
    // Main thread
5147
    //************************************************************************************************
5148
    for (size_t iter = 0; iter < 20 + TEST_DURATION * TEST_DURATION * 500; iter++) {
×
5149
        vector_mutex.lock();
×
5150
        rt->promote_to_write();
×
5151
        rt->commit_and_continue_as_read();
×
5152
        if (qs.size() < 100) {
×
5153
            for (size_t t = 0; t < 5; t++) {
×
5154
                auto t2 = rt->duplicate();
×
5155
                qs.push_back(t2->import_copy_of(query, PayloadPolicy::Move));
×
5156
                vids.push_back(t2);
×
5157
            }
×
5158
        }
×
5159
        vector_mutex.unlock();
×
5160

5161
        millisleep(100);
×
5162
    }
×
5163
    //************************************************************************************************
5164

5165
    end_signal = true;
×
5166
    for (int i = 0; i != threads; ++i)
×
5167
        slaves[i].join();
×
5168
}
×
5169

5170

5171
// TableView::clear() was originally reported to be slow when table was indexed and had links, but performance
5172
// has now doubled. This test is just a short sanity test that clear() still works.
5173
TEST(LangBindHelper_TableViewClear)
5174
{
2✔
5175
    SHARED_GROUP_TEST_PATH(path);
2✔
5176

1✔
5177
    int64_t number_of_history = 1000;
2✔
5178
    int64_t number_of_line = 18;
2✔
5179

1✔
5180
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5181
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5182
    TransactionRef tr;
2✔
5183
    ColKey col0, col1, col2, colA, colB;
2✔
5184
    // set up tables:
1✔
5185
    // history : ["id" (int), "parent" (int), "lines" (list(line))]
1✔
5186
    // line    : ["id" (int), "parent" (int)]
1✔
5187
    {
2✔
5188
        tr = sg->start_write();
2✔
5189

1✔
5190
        TableRef history = tr->add_table("history");
2✔
5191
        TableRef line = tr->add_table("line");
2✔
5192

1✔
5193
        col0 = history->add_column(type_Int, "id");
2✔
5194
        col1 = history->add_column(type_Int, "parent");
2✔
5195
        col2 = history->add_column_list(*line, "lines");
2✔
5196
        history->add_search_index(col1);
2✔
5197

1✔
5198
        colA = line->add_column(type_Int, "id");
2✔
5199
        colB = line->add_column(type_Int, "parent");
2✔
5200
        line->add_search_index(colB);
2✔
5201
        tr->commit_and_continue_as_read();
2✔
5202
    }
2✔
5203

1✔
5204
    {
2✔
5205
        tr->promote_to_write();
2✔
5206

1✔
5207
        TableRef history = tr->get_table("history");
2✔
5208
        TableRef line = tr->get_table("line");
2✔
5209

1✔
5210
        auto obj = history->create_object();
2✔
5211
        obj.set(col0, 1);
2✔
5212
        auto ll = obj.get_linklist(col2);
2✔
5213
        for (int64_t j = 0; j < number_of_line; ++j) {
38✔
5214
            Obj o = line->create_object().set_all(j, 0);
36✔
5215
            ll.add(o.get_key());
36✔
5216
        }
36✔
5217

1✔
5218
        for (int64_t i = 1; i < number_of_history; ++i) {
2,000✔
5219
            history->create_object().set_all(i, i + 1);
1,998✔
5220
            int64_t rj = i * number_of_line;
1,998✔
5221
            for (int64_t j = 1; j <= number_of_line; ++j) {
37,962✔
5222
                line->create_object().set_all(rj, j);
35,964✔
5223
                ++rj;
35,964✔
5224
            }
35,964✔
5225
        }
1,998✔
5226
        tr->commit_and_continue_as_read();
2✔
5227
        CHECK_EQUAL(number_of_history, history->size());
2✔
5228
        CHECK_EQUAL(number_of_history * number_of_line, line->size());
2✔
5229
    }
2✔
5230

1✔
5231
    // query and delete
1✔
5232
    {
2✔
5233
        tr->promote_to_write();
2✔
5234

1✔
5235
        TableRef line = tr->get_table("line");
2✔
5236

1✔
5237
        //    number_of_line = 2;
1✔
5238
        for (int64_t i = 1; i <= number_of_line; ++i) {
38✔
5239
            TableView tv = (line->column<Int>(colB) == i).find_all();
36✔
5240
            tv.clear();
36✔
5241
        }
36✔
5242
        tr->commit_and_continue_as_read();
2✔
5243
    }
2✔
5244

1✔
5245
    {
2✔
5246
        TableRef history = tr->get_table("history");
2✔
5247
        TableRef line = tr->get_table("line");
2✔
5248

1✔
5249
        CHECK_EQUAL(number_of_history, history->size());
2✔
5250
        CHECK_EQUAL(number_of_line, line->size());
2✔
5251
    }
2✔
5252
}
2✔
5253

5254

5255
TEST(LangBindHelper_SessionHistoryConsistency)
5256
{
2✔
5257
    // Check that we can reliably detect inconsist history
1✔
5258
    // types across concurrent session participants.
1✔
5259

1✔
5260
    // Errors of this kind are considered as incorrect API usage, and will lead
1✔
5261
    // to throwing of LogicError exceptions.
1✔
5262

1✔
5263
    SHARED_GROUP_TEST_PATH(path);
2✔
5264

1✔
5265
    // When starting with an empty Realm, all history types are allowed, but all
1✔
5266
    // session participants must still agree
1✔
5267
    {
2✔
5268
        // No history
1✔
5269
        DBRef sg = DB::create(path, false, DBOptions(crypt_key()));
2✔
5270

1✔
5271
        // Out-of-Realm history
1✔
5272
        std::unique_ptr<Replication> hist = realm::make_in_realm_history();
2✔
5273
        CHECK_RUNTIME_ERROR(DB::create(*hist, path, DBOptions(crypt_key())), ErrorCodes::IncompatibleSession);
2✔
5274
    }
2✔
5275
}
2✔
5276

5277

5278
TEST(LangBindHelper_InRealmHistory_Upgrade)
5279
{
2✔
5280
    SHARED_GROUP_TEST_PATH(path_1);
2✔
5281
    {
2✔
5282
        // Out-of-Realm history
1✔
5283
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5284
        DBRef sg = DB::create(*hist, path_1, DBOptions(crypt_key()));
2✔
5285
        WriteTransaction wt(sg);
2✔
5286
        wt.commit();
2✔
5287
    }
2✔
5288
    {
2✔
5289
        // In-Realm history
1✔
5290
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5291
        DBRef sg = DB::create(*hist, path_1, DBOptions(crypt_key()));
2✔
5292
        WriteTransaction wt(sg);
2✔
5293
        wt.commit();
2✔
5294
    }
2✔
5295
    SHARED_GROUP_TEST_PATH(path_2);
2✔
5296
    {
2✔
5297
        // No history
1✔
5298
        DBRef sg = DB::create(path_2, false, DBOptions(crypt_key()));
2✔
5299
        WriteTransaction wt(sg);
2✔
5300
        wt.commit();
2✔
5301
    }
2✔
5302
    {
2✔
5303
        // In-Realm history
1✔
5304
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5305
        DBRef sg = DB::create(*hist, path_2, DBOptions(crypt_key()));
2✔
5306
        WriteTransaction wt(sg);
2✔
5307
        wt.commit();
2✔
5308
    }
2✔
5309
}
2✔
5310

5311
TEST(LangBindHelper_InRealmHistory_Downgrade)
5312
{
2✔
5313
    SHARED_GROUP_TEST_PATH(path);
2✔
5314
    {
2✔
5315
        // In-Realm history
1✔
5316
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5317
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5318
        WriteTransaction wt(sg);
2✔
5319
        wt.commit();
2✔
5320
    }
2✔
5321
    {
2✔
5322
        // No history
1✔
5323
        CHECK_THROW(DB::create(path, false, DBOptions(crypt_key())), IncompatibleHistories);
2✔
5324
    }
2✔
5325
}
2✔
5326

5327
// Trigger erase_rows with num_rows == 0 by inserting zero rows
5328
// and then rolling back the transaction. There was a problem
5329
// where accessors were not updated correctly in this case because
5330
// of an early out when num_rows_to_erase is zero.
5331
TEST(LangBindHelper_RollbackInsertZeroRows)
5332
{
2✔
5333
    SHARED_GROUP_TEST_PATH(path)
2✔
5334
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5335
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5336
    auto g = sg->start_write();
2✔
5337

1✔
5338
    auto t0 = g->add_table("t0");
2✔
5339
    auto t1 = g->add_table("t1");
2✔
5340

1✔
5341
    auto col = t0->add_column(*t1, "t0_link_to_t1");
2✔
5342
    t0->create_object();
2✔
5343
    auto o1 = t0->create_object();
2✔
5344
    t1->create_object();
2✔
5345
    auto v1 = t1->create_object();
2✔
5346
    o1.set(col, v1.get_key());
2✔
5347

1✔
5348
    CHECK_EQUAL(t0->size(), 2);
2✔
5349
    CHECK_EQUAL(t1->size(), 2);
2✔
5350
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5351

1✔
5352
    g->commit_and_continue_as_read();
2✔
5353
    g->promote_to_write();
2✔
5354

1✔
5355
    std::vector<ObjKey> keys;
2✔
5356
    t1->create_objects(0, keys); // Insert zero rows
2✔
5357

1✔
5358
    CHECK_EQUAL(t0->size(), 2);
2✔
5359
    CHECK_EQUAL(t1->size(), 2);
2✔
5360
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5361

1✔
5362
    g->rollback_and_continue_as_read();
2✔
5363
    g->verify();
2✔
5364

1✔
5365
    CHECK_EQUAL(t0->size(), 2);
2✔
5366
    CHECK_EQUAL(t1->size(), 2);
2✔
5367
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5368
}
2✔
5369

5370

5371
TEST(LangBindHelper_RollbackRemoveZeroRows)
5372
{
2✔
5373
    SHARED_GROUP_TEST_PATH(path)
2✔
5374
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5375
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5376
    auto g = sg->start_write();
2✔
5377

1✔
5378
    auto t0 = g->add_table("t0");
2✔
5379
    auto t1 = g->add_table("t1");
2✔
5380

1✔
5381
    auto col = t0->add_column(*t1, "t0_link_to_t1");
2✔
5382
    t0->create_object();
2✔
5383
    auto o1 = t0->create_object();
2✔
5384
    t1->create_object();
2✔
5385
    auto v1 = t1->create_object();
2✔
5386
    o1.set(col, v1.get_key());
2✔
5387

1✔
5388
    CHECK_EQUAL(t0->size(), 2);
2✔
5389
    CHECK_EQUAL(t1->size(), 2);
2✔
5390
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5391

1✔
5392
    g->commit_and_continue_as_read();
2✔
5393
    g->promote_to_write();
2✔
5394

1✔
5395
    t1->clear();
2✔
5396

1✔
5397
    CHECK_EQUAL(t0->size(), 2);
2✔
5398
    CHECK_EQUAL(t1->size(), 0);
2✔
5399
    CHECK_EQUAL(o1.get<ObjKey>(col), ObjKey());
2✔
5400

1✔
5401
    g->rollback_and_continue_as_read();
2✔
5402
    g->verify();
2✔
5403

1✔
5404
    CHECK_EQUAL(t0->size(), 2);
2✔
5405
    CHECK_EQUAL(t1->size(), 2);
2✔
5406
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5407
}
2✔
5408

5409
// Bug found by AFL during development of TimestampColumn
5410
TEST_TYPES(LangBindHelper_AddEmptyRowsAndRollBackTimestamp, std::true_type, std::false_type)
5411
{
4✔
5412
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5413
    SHARED_GROUP_TEST_PATH(path);
4✔
5414
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5415
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5416
    auto g = sg_w->start_write();
4✔
5417
    TableRef t = g->add_table("");
4✔
5418
    t->add_column(type_Int, "", nullable_toggle);
4✔
5419
    t->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5420
    g->commit_and_continue_as_read();
4✔
5421
    g->promote_to_write();
4✔
5422
    std::vector<ObjKey> keys;
4✔
5423
    t->create_objects(224, keys);
4✔
5424
    g->rollback_and_continue_as_read();
4✔
5425
    g->verify();
4✔
5426
}
4✔
5427

5428
// Another bug found by AFL during development of TimestampColumn
5429
TEST_TYPES(LangBindHelper_EmptyWrites, std::true_type, std::false_type)
5430
{
4✔
5431
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5432
    SHARED_GROUP_TEST_PATH(path);
4✔
5433
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5434
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5435
    auto g = sg_w->start_write();
4✔
5436
    TableRef t = g->add_table("");
4✔
5437
    t->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5438

2✔
5439
    for (int i = 0; i < 27; ++i) {
112✔
5440
        g->commit_and_continue_as_read();
108✔
5441
        g->promote_to_write();
108✔
5442
    }
108✔
5443

2✔
5444
    t->create_object();
4✔
5445
}
4✔
5446

5447

5448
// Found by AFL
5449
TEST_TYPES(LangBindHelper_SetTimestampRollback, std::true_type, std::false_type)
5450
{
4✔
5451
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5452
    SHARED_GROUP_TEST_PATH(path);
4✔
5453
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5454
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5455
    auto g = sg->start_write();
4✔
5456
    auto table = g->add_table("");
4✔
5457
    table->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5458
    table->create_object().set_all(Timestamp(-1, -1));
4✔
5459
    g->rollback_and_continue_as_read();
4✔
5460
    g->verify();
4✔
5461
}
4✔
5462

5463

5464
// Found by AFL, probably related to the rollback version above
5465
TEST_TYPES(LangBindHelper_SetTimestampAdvanceRead, std::true_type, std::false_type)
5466
{
4✔
5467
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5468
    SHARED_GROUP_TEST_PATH(path);
4✔
5469
    std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
5470
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
5471
    auto g_r = sg->start_read();
4✔
5472
    auto g_w = sg->start_write();
4✔
5473
    auto table = g_w->add_table("");
4✔
5474
    table->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5475
    table->create_object().set_all(Timestamp(-1, -1));
4✔
5476
    g_w->commit_and_continue_as_read();
4✔
5477
    g_w->verify();
4✔
5478
    g_r->advance_read();
4✔
5479
    g_r->verify();
4✔
5480
}
4✔
5481

5482

5483
// Found by AFL.
5484
TEST(LangbindHelper_BoolSearchIndexCommitPromote)
5485
{
2✔
5486
    SHARED_GROUP_TEST_PATH(path);
2✔
5487
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5488
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5489
    auto g = sg->start_write();
2✔
5490
    auto t = g->add_table("");
2✔
5491
    auto col = t->add_column(type_Bool, "gnyf", true);
2✔
5492
    std::vector<ObjKey> keys;
2✔
5493
    t->create_objects(5, keys);
2✔
5494
    t->get_object(keys[0]).set(col, false);
2✔
5495
    t->add_search_index(col);
2✔
5496
    g->commit_and_continue_as_read();
2✔
5497
    g->promote_to_write();
2✔
5498
    t->create_objects(5, keys);
2✔
5499
    t->remove_object(keys[8]);
2✔
5500
}
2✔
5501

5502

5503
// Found by AFL.
5504
TEST(LangbindHelper_GroupWriter_EdgeCaseAssert)
5505
{
2✔
5506
    SHARED_GROUP_TEST_PATH(path);
2✔
5507
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5508
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5509
    auto g_r = sg->start_read();
2✔
5510
    auto g_w = sg->start_write();
2✔
5511

1✔
5512
    auto t1 = g_w->add_table("dgrpnpgmjbchktdgagmqlihjckcdhpjccsjhnqlcjnbterse");
2✔
5513
    auto t2 = g_w->add_table("pknglaqnckqbffehqfgjnrepcfohoedkhiqsiedlotmaqitm");
2✔
5514
    t1->add_column(type_Double, "ggotpkoshbrcrmmqbagbfjetajlrrlbpjhhqrngfgdteilmj", true);
2✔
5515
    t2->add_column_list(*t1, "dtkiipajqdsfglbptieibknaoeeohqdlhftqmlriphobspjr");
2✔
5516
    std::vector<ObjKey> keys;
2✔
5517
    t1->create_objects(375, keys);
2✔
5518
    g_w->add_table("pnsidlijqeddnsgaesiijrrqedkdktmfekftogjccerhpeil");
2✔
5519
    g_r->close();
2✔
5520
    g_w->commit();
2✔
5521
    REALM_ASSERT_RELEASE(sg->compact());
2✔
5522
    g_w = sg->start_write();
2✔
5523
    g_r = sg->start_read();
2✔
5524
    g_r->verify();
2✔
5525
    g_w->add_table("citdgiaclkfbbksfaqegcfiqcserceaqmttkilnlbknoadtb");
2✔
5526
    g_w->add_table("tqtnnikpggeakeqcqhfqtshmimtjqkchgbnmbpttbetlahfi");
2✔
5527
    g_w->add_table("hkesaecjqbkemmmkffctacsnskekjbtqmpoetjnqkpactenf");
2✔
5528
    g_r->close();
2✔
5529
    g_w->commit();
2✔
5530
}
2✔
5531

5532
TEST(LangBindHelper_Bug2321)
5533
{
2✔
5534
    SHARED_GROUP_TEST_PATH(path);
2✔
5535
    ShortCircuitHistory hist;
2✔
5536
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
5537
    int i;
2✔
5538
    std::vector<ObjKey> target_keys;
2✔
5539
    std::vector<ObjKey> origin_keys;
2✔
5540
    ColKey col;
2✔
5541
    {
2✔
5542
        WriteTransaction wt(sg);
2✔
5543
        Group& group = wt.get_group();
2✔
5544
        TableRef target = group.add_table("target");
2✔
5545
        target->add_column(type_Int, "data");
2✔
5546
        target->create_objects(REALM_MAX_BPNODE_SIZE + 2, target_keys);
2✔
5547
        TableRef origin = group.add_table("origin");
2✔
5548
        col = origin->add_column_list(*target, "_link");
2✔
5549
        origin->create_objects(2, origin_keys);
2✔
5550
        wt.commit();
2✔
5551
    }
2✔
5552

1✔
5553
    {
2✔
5554
        WriteTransaction wt(sg);
2✔
5555
        Group& group = wt.get_group();
2✔
5556
        TableRef origin = group.get_table("origin");
2✔
5557
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5558
        for (i = 0; i < (REALM_MAX_BPNODE_SIZE - 1); i++) {
2,000✔
5559
            lv0.add(target_keys[i]);
1,998✔
5560
        }
1,998✔
5561
        wt.commit();
2✔
5562
    }
2✔
5563

1✔
5564
    auto reader = sg->start_read();
2✔
5565
    auto lv1 = reader->get_table("origin")->begin()->get_linklist(col);
2✔
5566
    {
2✔
5567
        WriteTransaction wt(sg);
2✔
5568
        Group& group = wt.get_group();
2✔
5569
        TableRef origin = group.get_table("origin");
2✔
5570
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5571
        lv0.add(target_keys[i++]);
2✔
5572
        lv0.add(target_keys[i++]);
2✔
5573
        wt.commit();
2✔
5574
    }
2✔
5575

1✔
5576
    // If MAX_BPNODE_SIZE is 4 and we run in debug mode, then the LinkView
1✔
5577
    // accessor was not refreshed correctly. It would still be a leaf class,
1✔
5578
    // but the header flags would tell it is a node.
1✔
5579
    reader->advance_read();
2✔
5580
    CHECK_EQUAL(lv1.size(), i);
2✔
5581
}
2✔
5582

5583
TEST(LangBindHelper_Bug2295)
5584
{
2✔
5585
    SHARED_GROUP_TEST_PATH(path);
2✔
5586
    ShortCircuitHistory hist;
2✔
5587
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
5588
    int i;
2✔
5589
    std::vector<ObjKey> target_keys;
2✔
5590
    std::vector<ObjKey> origin_keys;
2✔
5591
    ColKey col;
2✔
5592
    {
2✔
5593
        WriteTransaction wt(sg);
2✔
5594
        Group& group = wt.get_group();
2✔
5595
        TableRef target = group.add_table("target");
2✔
5596
        target->add_column(type_Int, "data");
2✔
5597
        target->create_objects(REALM_MAX_BPNODE_SIZE + 2, target_keys);
2✔
5598
        TableRef origin = group.add_table("origin");
2✔
5599
        col = origin->add_column_list(*target, "_link");
2✔
5600
        origin->create_objects(2, origin_keys);
2✔
5601
        wt.commit();
2✔
5602
    }
2✔
5603

1✔
5604
    {
2✔
5605
        WriteTransaction wt(sg);
2✔
5606
        Group& group = wt.get_group();
2✔
5607
        TableRef origin = group.get_table("origin");
2✔
5608
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5609
        for (i = 0; i < (REALM_MAX_BPNODE_SIZE - 1); i++) {
2,000✔
5610
            lv0.add(target_keys[i]);
1,998✔
5611
        }
1,998✔
5612
        wt.commit();
2✔
5613
    }
2✔
5614

1✔
5615
    auto reader = sg->start_read();
2✔
5616
    auto lv1 = reader->get_table("origin")->begin()->get_linklist(col);
2✔
5617
    CHECK_EQUAL(lv1.size(), i);
2✔
5618
    {
2✔
5619
        WriteTransaction wt(sg);
2✔
5620
        Group& group = wt.get_group();
2✔
5621
        TableRef origin = group.get_table("origin");
2✔
5622
        // With the error present, this will cause some areas to be freed
1✔
5623
        // that has already been freed in the above transaction
1✔
5624
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5625
        lv0.add(target_keys[i++]);
2✔
5626
        wt.commit();
2✔
5627
    }
2✔
5628
    reader->promote_to_write();
2✔
5629
    // Here we write the duplicates to the free list
1✔
5630
    reader->commit_and_continue_as_read();
2✔
5631
    reader->verify();
2✔
5632
    CHECK_EQUAL(lv1.size(), i);
2✔
5633
}
2✔
5634

5635
#ifdef LEGACY_TESTS // FIXME: Requires get_at() method to be available on Obj.
5636
ONLY(LangBindHelper_BigBinary)
5637
{
5638
    SHARED_GROUP_TEST_PATH(path);
5639
    ShortCircuitHistory hist;
5640
    DBRef sg = DB::create(hist, path);
5641
    std::string big_data(0x1000000, 'x');
5642
    auto rt = sg->start_read();
5643
    auto wt = sg->start_write();
5644

5645
    std::string data(16777362, 'y');
5646
    TableRef target = wt->add_table("big");
5647
    auto col = target->add_column(type_Binary, "data");
5648
    target->create_object().set(col, BinaryData(data.data(), data.size()));
5649
    wt->commit();
5650
    rt->advance_read();
5651
    {
5652
        WriteTransaction wt(sg);
5653
        TableRef t = wt.get_table("big");
5654
        t->begin()->set(col, BinaryData(big_data.data(), big_data.size()));
5655
        wt.get_group().verify();
5656
        wt.commit();
5657
    }
5658
    rt->advance_read();
5659
    auto t = rt->get_table("big");
5660
    size_t pos = 0;
5661
    BinaryData bin = t->begin()->get_at(col, pos); // <---- not there yet?
5662
    CHECK_EQUAL(memcmp(big_data.data(), bin.data(), bin.size()), 0);
5663
}
5664
#endif
5665

5666
TEST(LangBindHelper_CopyOnWriteOverflow)
5667
{
2✔
5668
    SHARED_GROUP_TEST_PATH(path);
2✔
5669
    ShortCircuitHistory hist;
2✔
5670
    DBRef sg = DB::create(hist, path);
2✔
5671
    auto g = sg->start_write();
2✔
5672
    auto table = g->add_table("big");
2✔
5673
    auto obj = table->create_object();
2✔
5674
    auto col = table->add_column(type_Binary, "data");
2✔
5675
    std::string data(0xfffff0, 'x');
2✔
5676
    obj.set(col, BinaryData(data.data(), data.size()));
2✔
5677
    g->commit();
2✔
5678
    g = sg->start_write();
2✔
5679
    g->get_table("big")->begin()->set(col, BinaryData{"Hello", 5});
2✔
5680
    g->verify();
2✔
5681
    g->commit();
2✔
5682
}
2✔
5683

5684

5685
TEST(LangBindHelper_RollbackOptimize)
5686
{
2✔
5687
    SHARED_GROUP_TEST_PATH(path);
2✔
5688
    const char* key = crypt_key();
2✔
5689
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5690
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(key));
2✔
5691
    auto g = sg_w->start_write();
2✔
5692

1✔
5693
    auto table = g->add_table("t0");
2✔
5694
    auto col = table->add_column(type_String, "str_col_0", true);
2✔
5695
    g->commit_and_continue_as_read();
2✔
5696
    g->verify();
2✔
5697
    g->promote_to_write();
2✔
5698
    g->verify();
2✔
5699
    std::vector<ObjKey> keys;
2✔
5700
    table->create_objects(198, keys);
2✔
5701
    table->enumerate_string_column(col);
2✔
5702
    g->rollback_and_continue_as_read();
2✔
5703
    g->verify();
2✔
5704
}
2✔
5705

5706

5707
TEST(LangBindHelper_BinaryReallocOverMax)
5708
{
2✔
5709
    SHARED_GROUP_TEST_PATH(path);
2✔
5710
    const char* key = crypt_key();
2✔
5711
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5712
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(key));
2✔
5713
    auto g = sg_w->start_write();
2✔
5714
    auto table = g->add_table("table");
2✔
5715
    auto col = table->add_column(type_Binary, "binary_col", false);
2✔
5716
    auto obj = table->create_object();
2✔
5717

1✔
5718
    // The sizes of these binaries were found with AFL. Essentially we must hit
1✔
5719
    // the case where doubling the allocated memory goes above max_array_payload
1✔
5720
    // and hits the condition to clamp to the maximum.
1✔
5721
    std::string blob1(8877637, static_cast<unsigned char>(133));
2✔
5722
    std::string blob2(15994373, static_cast<unsigned char>(133));
2✔
5723
    BinaryData dataAlloc(blob1);
2✔
5724
    BinaryData dataRealloc(blob2);
2✔
5725

1✔
5726
    obj.set(col, dataAlloc);
2✔
5727
    obj.set(col, dataRealloc);
2✔
5728
    g->verify();
2✔
5729
}
2✔
5730

5731

5732
// This test verifies that small unencrypted files are treated correctly if
5733
// opened as encrypted.
5734
#if REALM_ENABLE_ENCRYPTION
5735
TEST(LangBindHelper_OpenAsEncrypted)
5736
{
2✔
5737
    SHARED_GROUP_TEST_PATH(path);
2✔
5738
    {
2✔
5739
        ShortCircuitHistory hist;
2✔
5740
        DBRef sg_clear = DB::create(hist, path);
2✔
5741

1✔
5742
        {
2✔
5743
            WriteTransaction wt(sg_clear);
2✔
5744
            TableRef target = wt.add_table("table");
2✔
5745
            target->add_column(type_String, "mixed_col");
2✔
5746
            target->create_object();
2✔
5747
            wt.commit();
2✔
5748
        }
2✔
5749
    }
2✔
5750
    {
2✔
5751
        const char* key = crypt_key(true);
2✔
5752
        std::unique_ptr<Replication> hist_encrypt(make_in_realm_history());
2✔
5753
        CHECK_THROW(DB::create(*hist_encrypt, path, DBOptions(key)), InvalidDatabase);
2✔
5754
    }
2✔
5755
}
2✔
5756
#endif
5757

5758

5759
// Test case generated in [realm-core-4.0.4] on Mon Dec 18 13:33:24 2017.
5760
// Adding 0 rows to a StringEnumColumn would add the default value to the keys
5761
// but not the indexes creating an inconsistency.
5762
TEST(LangBindHelper_EnumColumnAddZeroRows)
5763
{
2✔
5764
    SHARED_GROUP_TEST_PATH(path);
2✔
5765
    const char* key = nullptr;
2✔
5766
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5767
    DBRef sg = DB::create(*hist, path, DBOptions(key));
2✔
5768
    auto g = sg->start_write();
2✔
5769
    auto g_r = sg->start_read();
2✔
5770
    auto table = g->add_table("");
2✔
5771

1✔
5772
    auto col = table->add_column(DataType(2), "table", false);
2✔
5773
    table->enumerate_string_column(col);
2✔
5774
    g->commit_and_continue_as_read();
2✔
5775
    g->verify();
2✔
5776
    g->promote_to_write();
2✔
5777
    g->verify();
2✔
5778
    table->create_object();
2✔
5779
    g->commit_and_continue_as_read();
2✔
5780
    g_r->advance_read();
2✔
5781
    g_r->verify();
2✔
5782
    g->verify();
2✔
5783
}
2✔
5784

5785

5786
TEST(LangBindHelper_RemoveObject)
5787
{
2✔
5788
    SHARED_GROUP_TEST_PATH(path);
2✔
5789
    ShortCircuitHistory hist;
2✔
5790
    DBRef sg = DB::create(hist, path);
2✔
5791
    ColKey col;
2✔
5792
    auto rt = sg->start_read();
2✔
5793
    {
2✔
5794
        auto wt = sg->start_write();
2✔
5795
        TableRef t = wt->add_table("Foo");
2✔
5796
        col = t->add_column(type_Int, "int");
2✔
5797
        t->create_object(ObjKey(123)).set(col, 1);
2✔
5798
        t->create_object(ObjKey(456)).set(col, 2);
2✔
5799
        wt->commit();
2✔
5800
    }
2✔
5801

1✔
5802
    rt->advance_read();
2✔
5803
    auto table = rt->get_table("Foo");
2✔
5804
    const Obj o1 = table->get_object(ObjKey(123));
2✔
5805
    const Obj o2 = table->get_object(ObjKey(456));
2✔
5806
    CHECK_EQUAL(o1.get<int64_t>(col), 1);
2✔
5807
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
5808

1✔
5809
    {
2✔
5810
        auto wt = sg->start_write();
2✔
5811
        TableRef t = wt->get_table("Foo");
2✔
5812
        t->remove_object(ObjKey(123));
2✔
5813
        wt->commit();
2✔
5814
    }
2✔
5815
    rt->advance_read();
2✔
5816
    CHECK_THROW(o1.get<int64_t>(col), KeyNotFound);
2✔
5817
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
5818
}
2✔
5819

5820
TEST(LangBindHelper_callWithLock)
5821
{
2✔
5822
    SHARED_GROUP_TEST_PATH(path);
2✔
5823
    auto callback = [&](const std::string& realm_path) {
4✔
5824
        CHECK(realm_path.compare(path) == 0);
4✔
5825
    };
4✔
5826

1✔
5827
    auto callback_not_called = [&](const std::string&) {
1✔
5828
        CHECK(false);
×
5829
    };
×
5830

1✔
5831
    // call_with_lock should run the callback if the lock file doesn't exist.
1✔
5832
    CHECK_NOT(File::exists(path.get_lock_path()));
2✔
5833
    CHECK(DB::call_with_lock(path, callback));
2✔
5834
    CHECK(File::exists(path.get_lock_path()));
2✔
5835

1✔
5836
    {
2✔
5837
        std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5838
        DBRef sg_w = DB::create(*hist_w, path);
2✔
5839
        WriteTransaction wt(sg_w);
2✔
5840
        CHECK_NOT(DB::call_with_lock(path, callback_not_called));
2✔
5841
        wt.commit();
2✔
5842
        CHECK_NOT(DB::call_with_lock(path, callback_not_called));
2✔
5843
    }
2✔
5844
    CHECK(DB::call_with_lock(path, callback));
2✔
5845
}
2✔
5846

5847
TEST(LangBindHelper_AdvanceReadCluster)
5848
{
2✔
5849
    SHARED_GROUP_TEST_PATH(path);
2✔
5850
    ShortCircuitHistory hist;
2✔
5851
    DBRef sg = DB::create(hist, path);
2✔
5852

1✔
5853
    auto rt = sg->start_read();
2✔
5854
    {
2✔
5855
        auto wt = sg->start_write();
2✔
5856
        TableRef t = wt->add_table("Foo");
2✔
5857
        auto int_col = t->add_column(type_Int, "int");
2✔
5858
        for (int64_t i = 0; i < 100; i++) {
202✔
5859
            t->create_object(ObjKey(i)).set(int_col, i);
200✔
5860
        }
200✔
5861
        wt->commit();
2✔
5862
    }
2✔
5863

1✔
5864
    rt->advance_read();
2✔
5865
    auto table = rt->get_table("Foo");
2✔
5866
    auto col = table->get_column_key("int");
2✔
5867
    for (int64_t i = 0; i < 100; i++) {
202✔
5868
        const Obj o = table->get_object(ObjKey(i));
200✔
5869
        CHECK_EQUAL(o.get<int64_t>(col), i);
200✔
5870
    }
200✔
5871
}
2✔
5872

5873
TEST(LangBindHelper_ImportDetachedLinkList)
5874
{
2✔
5875
    SHARED_GROUP_TEST_PATH(path);
2✔
5876
    auto hist = make_in_realm_history();
2✔
5877
    DBRef db = DB::create(*hist, path);
2✔
5878
    std::unique_ptr<TableView> tv_1;
2✔
5879

1✔
5880
    ColKey col_pet;
2✔
5881
    ColKey col_addr;
2✔
5882
    ColKey col_name;
2✔
5883
    ColKey col_age;
2✔
5884

1✔
5885
    {
2✔
5886
        WriteTransaction wt(db);
2✔
5887
        auto persons = wt.add_table("person");
2✔
5888
        auto dogs = wt.add_table("dog");
2✔
5889
        col_pet = persons->add_column_list(*dogs, "pet");
2✔
5890
        col_addr = persons->add_column_list(type_String, "address");
2✔
5891
        col_name = dogs->add_column(type_String, "name");
2✔
5892
        col_age = dogs->add_column(type_Int, "age");
2✔
5893

1✔
5894
        auto tago = dogs->create_object().set(col_name, "Tago").set(col_age, 9);
2✔
5895
        auto hector = dogs->create_object().set(col_name, "Hector").set(col_age, 7);
2✔
5896

1✔
5897
        auto me = persons->create_object();
2✔
5898
        me.set_list_values<String>(col_addr, {"Paradisæblevej 5", "2500 Andeby"});
2✔
5899
        auto pets = me.get_linklist(col_pet);
2✔
5900
        pets.add(tago.get_key());
2✔
5901
        pets.add(hector.get_key());
2✔
5902
        wt.commit();
2✔
5903
    }
2✔
5904

1✔
5905
    auto rt = db->start_read();
2✔
5906
    auto persons = rt->get_table("person");
2✔
5907
    auto dogs = rt->get_table("dog");
2✔
5908
    Obj me = *persons->begin();
2✔
5909
    auto my_pets = me.get_linklist(col_pet);
2✔
5910
    Query q = dogs->where(my_pets).equal(col_age, 7);
2✔
5911
    auto tv = q.find_all();
2✔
5912
    CHECK_EQUAL(tv.size(), 1);
2✔
5913
    auto my_address = me.get_listbase_ptr(col_addr);
2✔
5914
    CHECK_EQUAL(my_address->size(), 2);
2✔
5915

1✔
5916
    {
2✔
5917
        // Delete the person.
1✔
5918
        WriteTransaction wt(db);
2✔
5919
        wt.get_table("person")->begin()->remove();
2✔
5920
        wt.commit();
2✔
5921
    }
2✔
5922

1✔
5923
    {
2✔
5924
        auto read_transaction = db->start_read();
2✔
5925

1✔
5926
        // The link_list that is embedded in the query imported here should be detached
1✔
5927
        auto local_tv = read_transaction->import_copy_of(tv, PayloadPolicy::Stay);
2✔
5928
        local_tv->sync_if_needed();
2✔
5929
        CHECK_EQUAL(local_tv->size(), 0);
2✔
5930

1✔
5931
        // Check that we can import a detached link_list back
1✔
5932
        tv_1 = rt->import_copy_of(*local_tv, PayloadPolicy::Move);
2✔
5933

1✔
5934
        // The list imported here should be null
1✔
5935
        CHECK_NOT(read_transaction->import_copy_of(*my_address));
2✔
5936
    }
2✔
5937

1✔
5938
    CHECK_EQUAL(tv_1->size(), 0);
2✔
5939
}
2✔
5940

5941
TEST(LangBindHelper_SearchIndexAccessor)
5942
{
2✔
5943
    SHARED_GROUP_TEST_PATH(path);
2✔
5944
    auto hist = make_in_realm_history();
2✔
5945
    DBRef db = DB::create(*hist, path);
2✔
5946
    ColKey col_name;
2✔
5947

1✔
5948
    auto tr = db->start_write();
2✔
5949
    {
2✔
5950
        auto persons = tr->add_table("person");
2✔
5951
        col_name = persons->add_column(type_String, "name");
2✔
5952
        persons->add_search_index(col_name);
2✔
5953
        persons->create_object().set(col_name, "Per");
2✔
5954
    }
2✔
5955
    tr->commit_and_continue_as_read();
2✔
5956

1✔
5957
    tr->promote_to_write();
2✔
5958
    {
2✔
5959
        auto persons = tr->get_table("person");
2✔
5960
        persons->remove_column(col_name);
2✔
5961
        auto col_age = persons->add_column(type_Int, "age");
2✔
5962
        persons->add_search_index(col_age);
2✔
5963
        // Index referring to col_age is now at position 0
1✔
5964
        persons->create_object().set(col_age, 47);
2✔
5965
    }
2✔
5966
    // The index accessor must be refreshed with old ColKey (col_name)
1✔
5967
    tr->rollback_and_continue_as_read();
2✔
5968

1✔
5969
    tr->promote_to_write();
2✔
5970
    {
2✔
5971
        auto persons = tr->get_table("person");
2✔
5972
        // Index accssor uses its ColKey to find value in table
1✔
5973
        persons->create_object().set(col_name, "Poul");
2✔
5974
    }
2✔
5975
    tr->commit();
2✔
5976
}
2✔
5977

5978
TEST(LangBindHelper_ArrayXoverMapping)
5979
{
2✔
5980
    SHARED_GROUP_TEST_PATH(path);
2✔
5981
    auto hist = make_in_realm_history();
2✔
5982
    DBRef db = DB::create(*hist, path);
2✔
5983
    ColKey my_col;
2✔
5984
    {
2✔
5985
        auto tr = db->start_write();
2✔
5986
        auto tbl = tr->add_table("my_table");
2✔
5987
        my_col = tbl->add_column(type_String, "my_col");
2✔
5988
        std::string s(1'000'000, 'a');
2✔
5989
        for (auto i = 0; i < 100; ++i)
202✔
5990
            tbl->create_object().set_all(s);
200✔
5991
        tr->commit();
2✔
5992
    }
2✔
5993
    REALM_ASSERT(db->compact());
2✔
5994
    {
2✔
5995
        auto tr = db->start_read();
2✔
5996
        auto tbl = tr->get_table("my_table");
2✔
5997
        for (auto i = 0; i < 100; ++i) {
202✔
5998
            auto o = tbl->get_object(i);
200✔
5999
            StringData str = o.get<String>(my_col);
200✔
6000
            for (auto j = 0; j < 1'000'000; ++j)
200,000,200✔
6001
                REALM_ASSERT(str[j] == 'a');
200✔
6002
        }
200✔
6003
    }
2✔
6004
}
2✔
6005

6006
TEST(LangBindHelper_SchemaChangeNotification)
6007
{
2✔
6008
    SHARED_GROUP_TEST_PATH(path);
2✔
6009
    auto hist = make_in_realm_history();
2✔
6010
    DBRef db = DB::create(*hist, path);
2✔
6011

1✔
6012
    auto rt = db->start_read();
2✔
6013
    bool handler_called;
2✔
6014
    rt->set_schema_change_notification_handler([&handler_called]() {
4✔
6015
        handler_called = true;
4✔
6016
    });
4✔
6017
    CHECK(rt->has_schema_change_notification_handler());
2✔
6018

1✔
6019
    {
2✔
6020
        auto tr = db->start_write();
2✔
6021
        tr->add_table("my_table");
2✔
6022
        tr->commit();
2✔
6023
    }
2✔
6024
    handler_called = false;
2✔
6025
    rt->advance_read();
2✔
6026
    CHECK(handler_called);
2✔
6027

1✔
6028
    {
2✔
6029
        auto tr = db->start_write();
2✔
6030
        auto table = tr->get_table("my_table");
2✔
6031
        table->add_column(type_Int, "integer");
2✔
6032
        tr->commit();
2✔
6033
    }
2✔
6034
    handler_called = false;
2✔
6035
    rt->advance_read();
2✔
6036
    CHECK(handler_called);
2✔
6037
}
2✔
6038

6039
TEST(LangBindHelper_InMemoryDB)
6040
{
2✔
6041
    DBRef sg = DB::create(make_in_realm_history());
2✔
6042
    ColKey col;
2✔
6043
    auto rt = sg->start_read();
2✔
6044
    {
2✔
6045
        auto wt = sg->start_write();
2✔
6046
        TableRef t = wt->add_table("Foo");
2✔
6047
        col = t->add_column(type_Int, "int");
2✔
6048
        t->create_object(ObjKey(123)).set(col, 1);
2✔
6049
        t->create_object(ObjKey(456)).set(col, 2);
2✔
6050
        wt->commit();
2✔
6051
    }
2✔
6052

1✔
6053
    rt->advance_read();
2✔
6054
    auto table = rt->get_table("Foo");
2✔
6055
    const Obj o1 = table->get_object(ObjKey(123));
2✔
6056
    const Obj o2 = table->get_object(ObjKey(456));
2✔
6057
    CHECK_EQUAL(o1.get<int64_t>(col), 1);
2✔
6058
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
6059

1✔
6060
    {
2✔
6061
        auto wt = sg->start_write();
2✔
6062
        TableRef t = wt->get_table("Foo");
2✔
6063
        t->remove_object(ObjKey(123));
2✔
6064
        wt->commit();
2✔
6065
    }
2✔
6066
    rt->advance_read();
2✔
6067
    CHECK_THROW(o1.get<int64_t>(col), KeyNotFound);
2✔
6068
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
6069
}
2✔
6070

6071
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc