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

realm / realm-core / 2035

14 Feb 2024 04:42PM UTC coverage: 91.851% (+0.02%) from 91.828%
2035

push

Evergreen

web-flow
Fix app URI tests for baasaas (#7342)

93044 of 171508 branches covered (54.25%)

156 of 166 new or added lines in 1 file covered. (93.98%)

49 existing lines in 10 files now uncovered.

235428 of 256314 relevant lines covered (91.85%)

6503264.99 hits per line

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

93.31
/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
{
198✔
78
    CHECK(frozen->is_frozen());
198✔
79
    CHECK_THROW(frozen->promote_to_write(), LogicError);
198✔
80
    auto table = frozen->get_table("my_table");
198✔
81
    CHECK(table->is_frozen());
198✔
82
    auto col = table->get_column_key("my_col_1");
198✔
83
    int64_t sum = 0;
198✔
84
    for (auto i : *table) {
171,119✔
85
        sum += i.get<int64_t>(col);
171,119✔
86
    }
171,119✔
87
    CHECK_EQUAL(sum, 1000 / 2 * 999);
198✔
88
    TableView tv = table->where().not_equal(col, 42).find_all();
198✔
89
    CHECK(tv.is_frozen());
198✔
90
}
198✔
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);
194✔
125
        });
194✔
126
    for (int j = 0; j < num_threads; ++j)
202✔
127
        frozen_workers[j].join();
200✔
128
}
2✔
129

130
TEST(Transactions_ConcurrentFrozenTableGetByName)
131
{
2✔
132
#if REALM_VALGRIND
133
    // This test is slow under valgrind. Additionally, there is
134
    // a --max-threads config of 5000 for all (concurrent) tests
135
    constexpr int num_threads = 3;
136
#else
137
    constexpr int num_threads = 1000;
2✔
138
#endif
2✔
139
    constexpr int num_tables = 3 * num_threads;
2✔
140
    SHARED_GROUP_TEST_PATH(path);
2✔
141
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
142
    DBRef db = DB::create(*hist_w, path);
2✔
143
    TransactionRef frozen;
2✔
144
    std::string table_names[num_tables];
2✔
145
    {
2✔
146
        auto wt = db->start_write();
2✔
147
        for (int j = 0; j < num_tables; ++j) {
6,002✔
148
            std::string name = "Table" + to_string(j);
6,000✔
149
            table_names[j] = name;
6,000✔
150
            wt->add_table(name);
6,000✔
151
        }
6,000✔
152
        wt->commit_and_continue_as_read();
2✔
153
        frozen = wt->freeze();
2✔
154
    }
2✔
155
    auto runner = [&](int first, int last) {
1,986✔
156
        millisleep(1);
1,986✔
157
        for (int j = first; j < last; ++j) {
1,900,149✔
158
            frozen->get_table(table_names[j]);
1,898,163✔
159
        }
1,898,163✔
160
    };
1,986✔
161
    std::thread threads[num_threads];
2✔
162
    for (int j = 0; j < num_threads; ++j) {
2,002✔
163
        threads[j] = std::thread(runner, j * 2, j * 2 + num_threads);
2,000✔
164
    }
2,000✔
165
    for (int j = 0; j < num_threads; ++j)
2,002✔
166
        threads[j].join();
2,000✔
167
}
2✔
168

169
TEST(Transactions_ReclaimFrozen)
170
{
2✔
171
    struct Entry {
2✔
172
        TransactionRef frozen;
2✔
173
        Obj o;
2✔
174
        int64_t value;
2✔
175
    };
2✔
176
    int num_pending_transactions = 100;
2✔
177
    int num_transactions_created = 1000;
2✔
178
    int num_objects = 200;
2✔
179
    int num_checks_pr_trans = 10;
2✔
180

1✔
181
    SHARED_GROUP_TEST_PATH(path);
2✔
182
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
183
    DBRef db = DB::create(*hist_w, path);
2✔
184
    std::vector<Entry> refs;
2✔
185
    refs.resize(num_pending_transactions);
2✔
186
    Random random(random_int<unsigned long>());
2✔
187

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

224
TEST(Transactions_ConcurrentFrozenTableGetByKey)
225
{
2✔
226
#if REALM_VALGRIND
227
    // This test is slow under valgrind. Additionally, there is
228
    // a --max-threads config of 5000 for all (concurrent) tests
229
    constexpr int num_threads = 3;
230
#else
231
    constexpr int num_threads = 1000;
2✔
232
#endif
2✔
233
    constexpr int num_tables = 3 * num_threads;
2✔
234
    SHARED_GROUP_TEST_PATH(path);
2✔
235
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
236
    DBRef db = DB::create(*hist_w, path);
2✔
237
    TransactionRef frozen;
2✔
238
    TableKey table_keys[num_tables];
2✔
239
    {
2✔
240
        auto wt = db->start_write();
2✔
241
        for (int j = 0; j < num_tables; ++j) {
6,002✔
242
            std::string name = "Table" + to_string(j);
6,000✔
243
            auto table = wt->add_table(name);
6,000✔
244
            table_keys[j] = table->get_key();
6,000✔
245
        }
6,000✔
246
        wt->commit_and_continue_as_read();
2✔
247
        frozen = wt->freeze();
2✔
248
    }
2✔
249
    auto runner = [&](int first, int last) {
1,986✔
250
        millisleep(1);
1,986✔
251
        for (int j = first; j < last; ++j) {
1,542,156✔
252
            auto table = frozen->get_table(table_keys[j]);
1,540,170✔
253
            CHECK(table->get_key() == table_keys[j]);
1,540,170✔
254
        }
1,540,170✔
255
    };
1,986✔
256
    std::thread threads[num_threads];
2✔
257
    for (int j = 0; j < num_threads; ++j) {
2,002✔
258
        threads[j] = std::thread(runner, j * 2, j * 2 + num_threads);
2,000✔
259
    }
2,000✔
260
    for (int j = 0; j < num_threads; ++j)
2,002✔
261
        threads[j].join();
2,000✔
262
}
2✔
263

264

265
TEST(Transactions_ConcurrentFrozenQueryAndObj)
266
{
2✔
267
    SHARED_GROUP_TEST_PATH(path);
2✔
268
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
269
    DBRef db = DB::create(*hist_w, path);
2✔
270
    TransactionRef frozen;
2✔
271
    ObjKey obj_keys[1000];
2✔
272
    {
2✔
273
        auto wt = db->start_write();
2✔
274
        auto table = wt->add_table("MyTable");
2✔
275
        table->add_column(type_Int, "MyCol");
2✔
276
        for (int i = 0; i < 1000; ++i) {
2,002✔
277
            obj_keys[i] = table->create_object().set_all(i).get_key();
2,000✔
278
        }
2,000✔
279
        wt->commit_and_continue_as_read();
2✔
280
        frozen = wt->freeze();
2✔
281
    }
2✔
282
    auto runner = [&](int first, int last) {
993✔
283
        millisleep(1);
993✔
284
        auto table = frozen->get_table("MyTable");
993✔
285
        auto col = table->get_column_key("MyCol");
993✔
286
        for (int j = first; j < last; ++j) {
430,930✔
287
            // loads of concurrent queries created and executed:
194,306✔
288
            TableView tb = table->where().equal(col, j).find_all();
429,937✔
289
            CHECK(tb.size() == 1);
429,937✔
290
            CHECK(tb.get_key(0) == obj_keys[j]);
429,937✔
291
            // concurrent reads from results are just fine:
194,306✔
292
            auto obj = tb[0];
429,937✔
293
            CHECK(obj.get<Int>(col) == j);
429,937✔
294
        }
429,937✔
295
    };
993✔
296
    std::thread threads[500];
2✔
297
    for (int j = 0; j < 500; ++j) {
1,002✔
298
        threads[j] = std::thread(runner, j, j + 500);
1,000✔
299
    }
1,000✔
300
    for (int j = 0; j < 500; ++j)
1,002✔
301
        threads[j].join();
1,000✔
302
}
2✔
303

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

361
class MyHistory : public _impl::History {
362
public:
363
    MyHistory(const MyHistory&) = delete;
364
    explicit MyHistory(MyHistory* write_history = nullptr)
365
        : m_write_history(write_history)
366
    {
104✔
367
    }
104✔
368
    std::vector<char> m_incoming_changeset;
369
    version_type m_incoming_version;
370
    struct ChangeSet {
371
        std::vector<char> changes;
372
        bool finalized = false;
373
    };
374
    std::map<uint_fast64_t, ChangeSet> m_changesets;
375
    MyHistory* m_write_history = nullptr;
376

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

422
    void verify() const override
423
    {
50✔
424
        // No-op
25✔
425
    }
50✔
426
};
427

428
class ShortCircuitHistory : public Replication {
429
public:
430
    using version_type = _impl::History::version_type;
431

432
    version_type prepare_changeset(const char* data, size_t size, version_type orig_version) override
433
    {
5,056✔
434
        return m_history.add_changeset(data, size, orig_version); // Throws
5,056✔
435
    }
5,056✔
436

437
    void finalize_changeset() noexcept override
438
    {
5,056✔
439
        m_history.finalize();
5,056✔
440
    }
5,056✔
441

442
    HistoryType get_history_type() const noexcept override
443
    {
20,286✔
444
        return hist_InRealm;
20,286✔
445
    }
20,286✔
446

447
    _impl::History* _get_history_write() override
448
    {
10,112✔
449
        return &m_history;
10,112✔
450
    }
10,112✔
451

452
    std::unique_ptr<_impl::History> _create_history_read() override
453
    {
74✔
454
        return std::make_unique<MyHistory>(&m_history);
74✔
455
    }
74✔
456

457
    int get_history_schema_version() const noexcept override
458
    {
38✔
459
        return 0;
38✔
460
    }
38✔
461

462
    bool is_upgradable_history_schema(int) const noexcept override
463
    {
×
464
        REALM_ASSERT(false);
×
465
        return false;
×
466
    }
×
467

468
    void upgrade_history_schema(int) override
469
    {
×
470
        REALM_ASSERT(false);
×
471
    }
×
472

473

474
private:
475
    MyHistory m_history;
476
};
477

478
} // anonymous namespace
479

480

481
TEST(LangBindHelper_AdvanceReadTransact_Basics)
482
{
2✔
483
    SHARED_GROUP_TEST_PATH(path);
2✔
484
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
485
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
486

1✔
487
    // Start a read transaction (to be repeatedly advanced)
1✔
488
    TransactionRef rt = sg->start_read();
2✔
489
    CHECK_EQUAL(0, rt->size());
2✔
490

1✔
491
    // Try to advance without anything having happened
1✔
492
    rt->advance_read();
2✔
493
    rt->verify();
2✔
494
    CHECK_EQUAL(0, rt->size());
2✔
495

1✔
496
    // Try to advance after an empty write transaction
1✔
497
    {
2✔
498
        WriteTransaction wt(sg);
2✔
499
        wt.commit();
2✔
500
    }
2✔
501
    rt->advance_read();
2✔
502
    rt->verify();
2✔
503
    CHECK_EQUAL(0, rt->size());
2✔
504

1✔
505
    // Try to advance after a superfluous rollback
1✔
506
    {
2✔
507
        WriteTransaction wt(sg);
2✔
508
        // Implicit rollback
1✔
509
    }
2✔
510
    rt->advance_read();
2✔
511
    rt->verify();
2✔
512
    CHECK_EQUAL(0, rt->size());
2✔
513

1✔
514
    // Try to advance after a propper rollback
1✔
515
    {
2✔
516
        WriteTransaction wt(sg);
2✔
517
        wt.add_table("bad");
2✔
518
        // Implicit rollback
1✔
519
    }
2✔
520
    rt->advance_read();
2✔
521
    rt->verify();
2✔
522
    CHECK_EQUAL(0, rt->size());
2✔
523

1✔
524
    // Create a table via the other SharedGroup
1✔
525
    ObjKey k0;
2✔
526
    {
2✔
527
        WriteTransaction wt(sg);
2✔
528
        TableRef foo_w = wt.add_table("foo");
2✔
529
        foo_w->add_column(type_Int, "i");
2✔
530
        k0 = foo_w->create_object().get_key();
2✔
531
        wt.commit();
2✔
532
    }
2✔
533

1✔
534
    rt->advance_read();
2✔
535
    rt->verify();
2✔
536
    CHECK_EQUAL(1, rt->size());
2✔
537
    ConstTableRef foo = rt->get_table("foo");
2✔
538
    CHECK_EQUAL(1, foo->get_column_count());
2✔
539
    auto cols = foo->get_column_keys();
2✔
540
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
541
    CHECK_EQUAL(1, foo->size());
2✔
542
    CHECK_EQUAL(0, foo->get_object(k0).get<int64_t>(cols[0]));
2✔
543
    uint_fast64_t version = foo->get_content_version();
2✔
544

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

1✔
593
    // Again, with no change
1✔
594
    rt->advance_read();
2✔
595
    rt->verify();
2✔
596
    CHECK_EQUAL(10, foo->get_column_count());
2✔
597
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
598
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
599
    CHECK_EQUAL(2, foo->size());
2✔
600
    CHECK_EQUAL(1, obj0.get<int64_t>(cols[0]));
2✔
601
    CHECK_EQUAL(2, obj1.get<int64_t>(cols[0]));
2✔
602
    CHECK_EQUAL("a", obj0.get<StringData>(cols[1]));
2✔
603
    CHECK_EQUAL("b", obj1.get<StringData>(cols[1]));
2✔
604
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
605

1✔
606
    // Perform several write transactions before advancing the read transaction
1✔
607
    {
2✔
608
        WriteTransaction wt(sg);
2✔
609
        TableRef bar_w = wt.add_table("bar");
2✔
610
        bar_w->add_column(type_Int, "a");
2✔
611
        wt.commit();
2✔
612
    }
2✔
613
    {
2✔
614
        WriteTransaction wt(sg);
2✔
615
        wt.commit();
2✔
616
    }
2✔
617
    {
2✔
618
        WriteTransaction wt(sg);
2✔
619
        TableRef bar_w = wt.get_table("bar");
2✔
620
        bar_w->add_column(type_Float, "b");
2✔
621
        wt.commit();
2✔
622
    }
2✔
623
    {
2✔
624
        WriteTransaction wt(sg);
2✔
625
        // Implicit rollback
1✔
626
    }
2✔
627
    {
2✔
628
        WriteTransaction wt(sg);
2✔
629
        TableRef bar_w = wt.get_table("bar");
2✔
630
        bar_w->add_column(type_Double, "c");
2✔
631
        wt.commit();
2✔
632
    }
2✔
633

1✔
634
    rt->advance_read();
2✔
635
    rt->verify();
2✔
636
    CHECK_EQUAL(2, rt->size());
2✔
637
    CHECK_EQUAL(10, foo->get_column_count());
2✔
638
    cols = foo->get_column_keys();
2✔
639
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
640
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
641
    CHECK_EQUAL(2, foo->size());
2✔
642
    CHECK_EQUAL(1, obj0.get<int64_t>(cols[0]));
2✔
643
    CHECK_EQUAL(2, obj1.get<int64_t>(cols[0]));
2✔
644
    CHECK_EQUAL("a", obj0.get<StringData>(cols[1]));
2✔
645
    CHECK_EQUAL("b", obj1.get<StringData>(cols[1]));
2✔
646
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
647
    ConstTableRef bar = rt->get_table("bar");
2✔
648
    cols = bar->get_column_keys();
2✔
649
    CHECK_EQUAL(3, bar->get_column_count());
2✔
650
    CHECK_EQUAL(type_Int, bar->get_column_type(cols[0]));
2✔
651
    CHECK_EQUAL(type_Float, bar->get_column_type(cols[1]));
2✔
652
    CHECK_EQUAL(type_Double, bar->get_column_type(cols[2]));
2✔
653

1✔
654
    // Clear tables - not supported before backlinks work again
1✔
655
    {
2✔
656
        WriteTransaction wt(sg);
2✔
657
        TableRef foo_w = wt.get_table("foo");
2✔
658
        foo_w->clear();
2✔
659
        TableRef bar_w = wt.get_table("bar");
2✔
660
        bar_w->clear();
2✔
661
        wt.commit();
2✔
662
    }
2✔
663
    rt->advance_read();
2✔
664
    rt->verify();
2✔
665

1✔
666
    size_t free_space, used_space;
2✔
667
    sg->get_stats(free_space, used_space);
2✔
668

1✔
669
    CHECK_EQUAL(2, rt->size());
2✔
670
    CHECK(foo);
2✔
671
    cols = foo->get_column_keys();
2✔
672
    CHECK_EQUAL(10, foo->get_column_count());
2✔
673
    CHECK_EQUAL(type_Int, foo->get_column_type(cols[0]));
2✔
674
    CHECK_EQUAL(type_String, foo->get_column_type(cols[1]));
2✔
675
    CHECK_EQUAL(0, foo->size());
2✔
676
    CHECK(bar);
2✔
677
    cols = bar->get_column_keys();
2✔
678
    CHECK_EQUAL(3, bar->get_column_count());
2✔
679
    CHECK_EQUAL(type_Int, bar->get_column_type(cols[0]));
2✔
680
    CHECK_EQUAL(type_Float, bar->get_column_type(cols[1]));
2✔
681
    CHECK_EQUAL(type_Double, bar->get_column_type(cols[2]));
2✔
682
    CHECK_EQUAL(0, bar->size());
2✔
683
    CHECK_EQUAL(foo, rt->get_table("foo"));
2✔
684
    CHECK_EQUAL(bar, rt->get_table("bar"));
2✔
685
}
2✔
686

687
TEST(LangBindHelper_AdvanceReadTransact_AddTableWithFreshSharedGroup)
688
{
2✔
689
    SHARED_GROUP_TEST_PATH(path);
2✔
690

1✔
691
    // Testing that a foreign transaction, that adds a table, can be applied to
1✔
692
    // a freshly created SharedGroup, even when another table existed in the
1✔
693
    // group prior to the one being added in the mentioned transaction. This
1✔
694
    // test is relevant because of the way table accesors are created and
1✔
695
    // managed inside a SharedGroup, in particular because table accessors are
1✔
696
    // created lazily, and will therefore not be present in a freshly created
1✔
697
    // SharedGroup instance.
1✔
698

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

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

1✔
713
    // Add the second table in a "foreign" transaction
1✔
714
    {
2✔
715
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
716
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
717
        WriteTransaction wt(sg_w);
2✔
718
        wt.add_table("table_2");
2✔
719
        wt.commit();
2✔
720
    }
2✔
721

1✔
722
    rt->advance_read();
2✔
723
}
2✔
724

725

726
TEST(LangBindHelper_AdvanceReadTransact_RemoveTableWithFreshSharedGroup)
727
{
2✔
728
    SHARED_GROUP_TEST_PATH(path);
2✔
729

1✔
730
    // Testing that a foreign transaction, that removes a table, can be applied
1✔
731
    // to a freshly created Sharedrt-> This test is relevant because of the
1✔
732
    // way table accesors are created and managed inside a SharedGroup, in
1✔
733
    // particular because table accessors are created lazily, and will therefore
1✔
734
    // not be present in a freshly created SharedGroup instance.
1✔
735

1✔
736
    // Add the table
1✔
737
    {
2✔
738
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
739
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
740
        WriteTransaction wt(sg_w);
2✔
741
        wt.add_table("table");
2✔
742
        wt.commit();
2✔
743
    }
2✔
744

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

1✔
750
    // remove the table in a "foreign" transaction
1✔
751
    {
2✔
752
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
753
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
754
        WriteTransaction wt(sg_w);
2✔
755
        wt.get_group().remove_table("table");
2✔
756
        wt.commit();
2✔
757
    }
2✔
758

1✔
759
    rt->advance_read();
2✔
760
}
2✔
761

762

763
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_CreateManyTables, testing_supports_spawn_process)
764
{
2✔
765
    SHARED_GROUP_TEST_PATH(path);
2✔
766
    SHARED_GROUP_TEST_PATH(path2);
2✔
767

1✔
768
    if (SpawnedProcess::is_parent()) {
2✔
769
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
770
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
771
        WriteTransaction wt(sg_w);
2✔
772
        wt.add_table("table");
2✔
773
        wt.commit();
2✔
774
    }
2✔
775

1✔
776
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
777
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
778
    TransactionRef rt = sg->start_read();
2✔
779

1✔
780
    auto process = test_util::spawn_process(test_context.test_details.test_name, "make_many_tables");
2✔
781
    if (process->is_child()) {
2✔
782
        size_t free_space, used_space;
×
783
        {
×
784
            std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
×
785
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
786

787
            WriteTransaction wt(sg_w);
×
788
            for (int i = 0; i < 16; ++i) {
×
789
                wt.add_table(util::format("table_%1", i));
×
790
            }
×
791
            wt.commit();
×
792
            sg_w->get_stats(free_space, used_space);
×
793
        }
×
794
        {
×
795
            std::unique_ptr<Replication> hist_w2(realm::make_in_realm_history());
×
796
            DBRef sg_w2 = DB::create(*hist_w2, path2, DBOptions(crypt_key()));
×
797
            WriteTransaction wt(sg_w2);
×
798
            auto table = wt.add_table("stats");
×
799
            ColKey col = table->add_column(type_Int, "used_space");
×
800
            table->create_object().set<int64_t>(col, used_space);
×
801
            wt.commit();
×
802
        }
×
803

804
        exit(0);
×
805
    }
×
806
    else {
2✔
807
        process->wait_for_child_to_finish();
2✔
808
    }
2✔
809
    size_t reported_used_space = 0;
2✔
810
    {
2✔
811
        std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
812
        DBRef sg = DB::create(*hist, path2, DBOptions(crypt_key()));
2✔
813
        WriteTransaction wt(sg);
2✔
814
        auto table = wt.get_table("stats");
2✔
815
        CHECK(table);
2✔
816
        CHECK_EQUAL(table->size(), 1);
2✔
817
        reported_used_space = size_t(table->begin()->get<int64_t>("used_space"));
2✔
818
    }
2✔
819

1✔
820
    rt->advance_read();
2✔
821
    auto used_space1 = rt->get_used_space();
2✔
822
    CHECK_EQUAL(reported_used_space, used_space1);
2✔
823
}
2✔
824

825

826
TEST(LangBindHelper_AdvanceReadTransact_PinnedSize)
827
{
2✔
828
    SHARED_GROUP_TEST_PATH(path);
2✔
829
    constexpr int num_rows = 1000;
2✔
830
    constexpr int iterations = 10;
2✔
831
    constexpr int rows_per_iteration = num_rows / iterations;
2✔
832

1✔
833
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
834
    auto sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
835
    ObjKeys keys;
2✔
836

1✔
837
    // Create some data
1✔
838
    {
2✔
839
        {
2✔
840
            WriteTransaction wt(sg);
2✔
841
            auto table = wt.add_table("table");
2✔
842
            table->add_column(type_Int, "int");
2✔
843
            wt.commit();
2✔
844
        }
2✔
845
        for (size_t i = 0; i < iterations; i++) {
22✔
846
            WriteTransaction wt(sg);
20✔
847
            auto table = wt.get_table("table");
20✔
848
            auto col = table->get_column_key("int");
20✔
849
            for (int j = 0; j < rows_per_iteration; j++) {
2,020✔
850
                auto k = table->create_object().set(col, j).get_key();
2,000✔
851
                keys.push_back(k);
2,000✔
852
            }
2,000✔
853
            wt.commit();
20✔
854
        }
20✔
855
    }
2✔
856

1✔
857
    // Pin this version
1✔
858
    auto rt = sg->start_read();
2✔
859
    size_t free_space, used_space, locked_space;
2✔
860

1✔
861
    // Make some more versions
1✔
862
    {
2✔
863
        for (int i = 0; i < iterations; i++) {
22✔
864
            WriteTransaction wt(sg);
20✔
865
            auto table = wt.get_table("table");
20✔
866
            auto col = table->get_column_key("int");
20✔
867
            for (int j = 0; j < rows_per_iteration; j++) {
2,020✔
868
                int ndx = rows_per_iteration * i + j;
2,000✔
869
                table->get_object(keys[ndx]).set(col, 2 * ndx);
2,000✔
870
            }
2,000✔
871
            wt.commit();
20✔
872
        }
20✔
873
        sg->get_stats(free_space, used_space, &locked_space);
2✔
874
    }
2✔
875

1✔
876
    CHECK_GREATER(locked_space, 0);
2✔
877
    CHECK_LESS(locked_space, free_space);
2✔
878

1✔
879
    // Cancel read transaction
1✔
880
    rt = nullptr;
2✔
881
    size_t new_locked_space;
2✔
882
    {
2✔
883
        WriteTransaction wt(sg);
2✔
884
        wt.commit();
2✔
885
        // Large history entries are freed here
1✔
886
    }
2✔
887
    {
2✔
888
        WriteTransaction wt(sg);
2✔
889
        wt.commit();
2✔
890
        // History entries still held by previous commit
1✔
891
    }
2✔
892
    {
2✔
893
        WriteTransaction wt(sg);
2✔
894
        wt.commit();
2✔
895
        // History entries now finally free
1✔
896
    }
2✔
897
    sg->get_stats(free_space, used_space, &new_locked_space);
2✔
898

1✔
899
    // Some space must have been released
1✔
900
    CHECK_LESS(new_locked_space, locked_space);
2✔
901
}
2✔
902

903

904
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_InsertTable, testing_supports_spawn_process)
905
{
2✔
906
    SHARED_GROUP_TEST_PATH(path);
2✔
907

1✔
908
    if (test_util::SpawnedProcess::is_parent()) {
2✔
909
        std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
2✔
910
        DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
911
        WriteTransaction wt(sg_w);
2✔
912

1✔
913
        TableRef table = wt.add_table("table1");
2✔
914
        table->add_column(type_Int, "col");
2✔
915

1✔
916
        table = wt.add_table("table2");
2✔
917
        table->add_column(type_Float, "col1");
2✔
918
        table->add_column(type_Float, "col2");
2✔
919

1✔
920
        wt.commit();
2✔
921
    }
2✔
922

1✔
923
    std::unique_ptr<Replication> hist(realm::make_in_realm_history());
2✔
924
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
925
    TransactionRef rt = sg->start_read();
2✔
926

1✔
927
    ConstTableRef table1 = rt->get_table("table1");
2✔
928
    ConstTableRef table2 = rt->get_table("table2");
2✔
929

1✔
930
    auto process = test_util::spawn_process(test_context.test_details.test_name, "add_table");
2✔
931
    if (process->is_child()) {
2✔
932
        {
×
933
            std::unique_ptr<Replication> hist_w(realm::make_in_realm_history());
×
934
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
935
            WriteTransaction wt(sg_w);
×
936
            wt.get_group().add_table("new table");
×
937
            wt.get_table("table1")->create_object();
×
938
            wt.get_table("table2")->create_object();
×
939
            wt.get_table("table2")->create_object();
×
940
            wt.commit();
×
941
        } // clean up sg before exit
×
942
        exit(0);
×
943
    }
×
944
    else {
2✔
945
        process->wait_for_child_to_finish();
2✔
946
    }
2✔
947

1✔
948
    rt->advance_read();
2✔
949

1✔
950
    CHECK_EQUAL(table1->size(), 1);
2✔
951
    CHECK_EQUAL(table2->size(), 2);
2✔
952
    CHECK_EQUAL(rt->get_table("new table")->size(), 0);
2✔
953
}
2✔
954

955
TEST(LangBindHelper_AdvanceReadTransact_LinkColumnInNewTable)
956
{
2✔
957
    // Verify that the table accessor of a link-opposite table is refreshed even
1✔
958
    // when the origin table is created in the same transaction as the link
1✔
959
    // column is added to it. This case is slightly involved, as there is a rule
1✔
960
    // that requires the two opposite table accessors of a link column (origin
1✔
961
    // and target sides) to either both exist or both not exist. On the other
1✔
962
    // hand, tables accessors are normally not created during
1✔
963
    // Group::advance_transact() for newly created tables.
1✔
964

1✔
965
    SHARED_GROUP_TEST_PATH(path);
2✔
966
    ShortCircuitHistory hist;
2✔
967
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
968
    DBRef sg_w = DB::create(hist, path, DBOptions(crypt_key()));
2✔
969
    {
2✔
970
        WriteTransaction wt(sg_w);
2✔
971
        wt.get_or_add_table("a");
2✔
972
        wt.commit();
2✔
973
    }
2✔
974

1✔
975
    TransactionRef rt = sg->start_read();
2✔
976
    ConstTableRef a_r = rt->get_table("a");
2✔
977

1✔
978
    {
2✔
979
        WriteTransaction wt(sg_w);
2✔
980
        TableRef a_w = wt.get_table("a");
2✔
981
        TableRef b_w = wt.get_or_add_table("b");
2✔
982
        b_w->add_column(*a_w, "foo");
2✔
983
        wt.commit();
2✔
984
    }
2✔
985

1✔
986
    rt->advance_read();
2✔
987
    CHECK(a_r);
2✔
988
    rt->verify();
2✔
989
}
2✔
990

991

992
TEST(LangBindHelper_AdvanceReadTransact_EnumeratedStrings)
993
{
2✔
994
    SHARED_GROUP_TEST_PATH(path);
2✔
995
    ShortCircuitHistory hist;
2✔
996
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
997
    ColKey c0, c1, c2;
2✔
998

1✔
999
    // Start a read transaction (to be repeatedly advanced)
1✔
1000
    auto rt = sg->start_read();
2✔
1001
    CHECK_EQUAL(0, rt->size());
2✔
1002

1✔
1003
    // Create 3 string columns, one primed for conversion to "unique string
1✔
1004
    // enumeration" representation
1✔
1005
    {
2✔
1006
        WriteTransaction wt(sg);
2✔
1007
        TableRef table_w = wt.add_table("t");
2✔
1008
        c0 = table_w->add_column(type_String, "a");
2✔
1009
        c1 = table_w->add_column(type_String, "b");
2✔
1010
        c2 = table_w->add_column(type_String, "c");
2✔
1011
        for (int i = 0; i < 1000; ++i) {
2,002✔
1012
            std::ostringstream out;
2,000✔
1013
            out << i;
2,000✔
1014
            std::string str = out.str();
2,000✔
1015
            table_w->create_object(ObjKey{}, {{c0, str}, {c1, "foo"}, {c2, str}});
2,000✔
1016
        }
2,000✔
1017
        wt.commit();
2✔
1018
    }
2✔
1019
    rt->advance_read();
2✔
1020
    rt->verify();
2✔
1021
    ConstTableRef table = rt->get_table("t");
2✔
1022
    CHECK_EQUAL(0, table->get_num_unique_values(c0));
2✔
1023
    CHECK_EQUAL(0, table->get_num_unique_values(c1)); // Not yet "optimized"
2✔
1024
    CHECK_EQUAL(0, table->get_num_unique_values(c2));
2✔
1025

1✔
1026
    // Optimize
1✔
1027
    {
2✔
1028
        WriteTransaction wt(sg);
2✔
1029
        TableRef table_w = wt.get_table("t");
2✔
1030
        table_w->enumerate_string_column(c1);
2✔
1031
        wt.commit();
2✔
1032
    }
2✔
1033
    rt->advance_read();
2✔
1034
    rt->verify();
2✔
1035
    CHECK_EQUAL(0, table->get_num_unique_values(c0));
2✔
1036
    CHECK_NOT_EQUAL(0, table->get_num_unique_values(c1)); // Must be "optimized" now
2✔
1037
    CHECK_EQUAL(0, table->get_num_unique_values(c2));
2✔
1038
}
2✔
1039

1040
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_SearchIndex, testing_supports_spawn_process)
1041
{
2✔
1042
    SHARED_GROUP_TEST_PATH(path);
2✔
1043
    if (test_util::SpawnedProcess::is_parent()) {
2✔
1044
        std::unique_ptr<Replication> hist_r = make_in_realm_history();
2✔
1045
        DBRef sg = DB::create(*hist_r, path, DBOptions(crypt_key()));
2✔
1046

1✔
1047
        // Start a read transaction (to be repeatedly advanced)
1✔
1048
        TransactionRef rt = sg->start_read();
2✔
1049
        CHECK_EQUAL(0, rt->size());
2✔
1050
    }
2✔
1051
    // Create 5 columns, and make 3 of them indexed
1✔
1052
    auto process = test_util::spawn_process(test_context.test_details.test_name, "init");
2✔
1053
    if (process->is_child()) {
2✔
1054
        {
×
1055
            std::vector<ObjKey> keys;
×
1056
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1057
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1058
            WriteTransaction wt(sg_w);
×
1059
            TableRef table_w = wt.add_table("t");
×
1060
            ColKey col_int = table_w->add_column(type_Int, "i0");
×
1061
            table_w->add_column(type_String, "s1");
×
1062
            ColKey col_str2 = table_w->add_column(type_String, "s2");
×
1063
            table_w->add_column(type_Int, "i3");
×
1064
            ColKey col_int4 = table_w->add_column(type_Int, "i4");
×
1065
            table_w->add_search_index(col_int);
×
1066
            table_w->add_search_index(col_str2);
×
1067
            table_w->add_search_index(col_int4);
×
1068
            table_w->create_objects(8, keys);
×
1069
            wt.commit();
×
1070
        } // clean up sg before exit
×
1071
        exit(0);
×
1072
    }
×
1073

1✔
1074
    if (process->is_parent()) {
2✔
1075
        process->wait_for_child_to_finish();
2✔
1076

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

1✔
1080
        // Start a read transaction (to be repeatedly advanced)
1✔
1081
        TransactionRef rt = sg->start_read();
2✔
1082
        rt->advance_read();
2✔
1083
        rt->verify();
2✔
1084
        ConstTableRef table = rt->get_table("t");
2✔
1085
        CHECK(table->has_search_index(table->get_column_key("i0")));
2✔
1086
        CHECK_NOT(table->has_search_index(table->get_column_key("s1")));
2✔
1087
        CHECK(table->has_search_index(table->get_column_key("s2")));
2✔
1088
        CHECK_NOT(table->has_search_index(table->get_column_key("i3")));
2✔
1089
        CHECK(table->has_search_index(table->get_column_key("i4")));
2✔
1090
    }
2✔
1091

1✔
1092
    // Remove the previous search indexes and add 2 new ones
1✔
1093
    process = test_util::spawn_process(test_context.test_details.test_name, "change_indexes");
2✔
1094
    if (process->is_child()) {
2✔
1095
        {
×
1096
            std::vector<ObjKey> keys;
×
1097
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1098
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1099
            WriteTransaction wt(sg_w);
×
1100
            TableRef table_w = wt.get_table("t");
×
1101
            table_w->create_objects(8, keys);
×
1102
            table_w->remove_search_index(table_w->get_column_key("s2"));
×
1103
            table_w->add_search_index(table_w->get_column_key("i3"));
×
1104
            table_w->remove_search_index(table_w->get_column_key("i0"));
×
1105
            table_w->add_search_index(table_w->get_column_key("s1"));
×
1106
            table_w->remove_search_index(table_w->get_column_key("i4"));
×
1107
            wt.commit();
×
1108
        }
×
1109
        exit(0);
×
1110
    }
×
1111

1✔
1112
    if (process->is_parent()) {
2✔
1113
        process->wait_for_child_to_finish();
2✔
1114

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

1✔
1118
        // Start a read transaction (to be repeatedly advanced)
1✔
1119
        TransactionRef rt = sg->start_read();
2✔
1120
        ConstTableRef table = rt->get_table("t");
2✔
1121
        rt->advance_read();
2✔
1122
        rt->verify();
2✔
1123
        CHECK_NOT(table->has_search_index(table->get_column_key("i0")));
2✔
1124
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1125
        CHECK_NOT(table->has_search_index(table->get_column_key("s2")));
2✔
1126
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1127
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1128
    }
2✔
1129

1✔
1130
    // Add some searchable contents
1✔
1131
    process = test_util::spawn_process(test_context.test_details.test_name, "add_content");
2✔
1132
    if (process->is_child()) {
2✔
1133
        {
×
1134
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1135
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1136
            WriteTransaction wt(sg_w);
×
1137
            TableRef table_w = wt.get_table("t");
×
1138
            int_fast64_t v = 7;
×
1139
            for (auto obj : *table_w) {
×
1140
                std::string out(util::to_string(v));
×
1141
                obj.set(table_w->get_column_key("s1"), StringData(out));
×
1142
                obj.set(table_w->get_column_key("i3"), v);
×
1143
                v = (v + 1581757577LL) % 1000;
×
1144
            }
×
1145
            wt.commit();
×
1146
        }
×
1147
        exit(0);
×
1148
    }
×
1149
    if (process->is_parent()) {
2✔
1150
        process->wait_for_child_to_finish();
2✔
1151

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

1✔
1155
        // Start a read transaction (to be repeatedly advanced)
1✔
1156
        TransactionRef rt = sg->start_read();
2✔
1157
        ConstTableRef table = rt->get_table("t");
2✔
1158
        rt->advance_read();
2✔
1159
        rt->verify();
2✔
1160

1✔
1161
        CHECK_NOT(table->has_search_index(table->get_column_key("i0")));
2✔
1162
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1163
        CHECK_NOT(table->has_search_index(table->get_column_key("s2")));
2✔
1164
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1165
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1166
        CHECK_EQUAL(ObjKey(12), table->find_first_string(table->get_column_key("s1"), "931"));
2✔
1167
        CHECK_EQUAL(ObjKey(4), table->find_first_int(table->get_column_key("i3"), 315));
2✔
1168
        CHECK_EQUAL(ObjKey(13), table->find_first_int(table->get_column_key("i3"), 508));
2✔
1169
    }
2✔
1170
    // Move the indexed columns by removal
1✔
1171
    process = test_util::spawn_process(test_context.test_details.test_name, "move_and_remove");
2✔
1172
    if (process->is_child()) {
2✔
1173
        {
×
1174
            std::unique_ptr<Replication> hist = make_in_realm_history();
×
1175
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1176
            WriteTransaction wt(sg_w);
×
1177
            TableRef table_w = wt.get_table("t");
×
1178
            table_w->remove_column(table_w->get_column_key("i0"));
×
1179
            table_w->remove_column(table_w->get_column_key("s2"));
×
1180
            wt.commit();
×
1181
        }
×
1182
        exit(0);
×
1183
    }
×
1184
    if (process->is_parent()) {
2✔
1185
        process->wait_for_child_to_finish();
2✔
1186

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

1✔
1190
        // Start a read transaction (to be repeatedly advanced)
1✔
1191
        TransactionRef rt = sg->start_read();
2✔
1192
        ConstTableRef table = rt->get_table("t");
2✔
1193
        rt->advance_read();
2✔
1194
        rt->verify();
2✔
1195
        CHECK(table->has_search_index(table->get_column_key("s1")));
2✔
1196
        CHECK(table->has_search_index(table->get_column_key("i3")));
2✔
1197
        CHECK_NOT(table->has_search_index(table->get_column_key("i4")));
2✔
1198
        CHECK_EQUAL(ObjKey(3), table->find_first_string(table->get_column_key("s1"), "738"));
2✔
1199
        CHECK_EQUAL(ObjKey(13), table->find_first_int(table->get_column_key("i3"), 508));
2✔
1200
    }
2✔
1201
}
2✔
1202

1203
TEST(LangBindHelper_AdvanceReadTransact_LinkView)
1204
{
2✔
1205
    SHARED_GROUP_TEST_PATH(path);
2✔
1206
    ShortCircuitHistory hist;
2✔
1207
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1208
    DBRef sg_w = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1209
    DBRef sg_q = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1210

1✔
1211
    // Start a continuous read transaction
1✔
1212
    TransactionRef rt = sg->start_read();
2✔
1213

1✔
1214
    // Add some tables and rows.
1✔
1215
    {
2✔
1216
        WriteTransaction wt(sg_w);
2✔
1217
        TableRef origin = wt.add_table("origin");
2✔
1218
        TableRef target = wt.add_table("target");
2✔
1219
        target->add_column(type_Int, "value");
2✔
1220
        auto col = origin->add_column_list(*target, "list");
2✔
1221

1✔
1222
        std::vector<ObjKey> keys;
2✔
1223
        target->create_objects(10, keys);
2✔
1224

1✔
1225
        Obj o0 = origin->create_object(ObjKey(0));
2✔
1226
        Obj o1 = origin->create_object(ObjKey(1));
2✔
1227

1✔
1228
        o0.get_linklist(col).add(keys[1]);
2✔
1229
        o1.get_linklist(col).add(keys[2]);
2✔
1230
        // state:
1✔
1231
        // origin[0].ll[0] -> target[1]
1✔
1232
        // origin[1].ll[0] -> target[2]
1✔
1233
        wt.commit();
2✔
1234
    }
2✔
1235
    rt->advance_read();
2✔
1236
    rt->verify();
2✔
1237

1✔
1238
    // Grab references to the LinkViews
1✔
1239
    auto origin = rt->get_table("origin");
2✔
1240
    auto col_link = origin->get_column_key("list");
2✔
1241
    const Obj obj0 = origin->get_object(ObjKey(0));
2✔
1242
    const Obj obj1 = origin->get_object(ObjKey(1));
2✔
1243

1✔
1244
    auto ll1 = obj0.get_linklist(col_link); // lv1[0] -> target[1]
2✔
1245
    auto ll2 = obj1.get_linklist(col_link); // lv2[0] -> target[2]
2✔
1246
    CHECK_EQUAL(ll1.size(), 1);
2✔
1247
    CHECK_EQUAL(ll2.size(), 1);
2✔
1248

1✔
1249
    ObjKey ll1_target = ll1.get_object(0).get_key();
2✔
1250
    CHECK_EQUAL(ll1.find_first(ll1_target), 0);
2✔
1251

1✔
1252
    {
2✔
1253
        WriteTransaction wt(sg_w);
2✔
1254
        wt.get_table("origin")->get_object(ObjKey(0)).get_linklist(col_link).clear();
2✔
1255
        wt.commit();
2✔
1256
    }
2✔
1257
    rt->advance_read();
2✔
1258
    rt->verify();
2✔
1259

1✔
1260
    CHECK_EQUAL(ll1.find_first(ll1_target), not_found);
2✔
1261
}
2✔
1262

1263
namespace {
1264

1265
template <typename T>
1266
class ConcurrentQueue {
1267
public:
1268
    ConcurrentQueue(size_t size)
1269
        : sz(size)
1270
    {
2✔
1271
        data.reset(new T[sz]);
2✔
1272
    }
2✔
1273
    inline bool is_full()
1274
    {
199,924✔
1275
        return writer - reader == sz;
199,924✔
1276
    }
199,924✔
1277
    inline bool is_empty()
1278
    {
214,057✔
1279
        return writer - reader == 0;
214,057✔
1280
    }
214,057✔
1281
    void put(T& e)
1282
    {
100,000✔
1283
        std::unique_lock<std::mutex> lock(mutex);
100,000✔
1284
        while (is_full())
100,000✔
UNCOV
1285
            not_full.wait(lock);
×
1286
        if (is_empty())
100,000✔
1287
            not_empty_or_closed.notify_all();
24,529✔
1288
        data[writer++ % sz] = std::move(e);
100,000✔
1289
    }
100,000✔
1290

1291
    bool get(T& e)
1292
    {
99,926✔
1293
        std::unique_lock<std::mutex> lock(mutex);
99,926✔
1294
        while (is_empty() && !closed)
114,057✔
1295
            not_empty_or_closed.wait(lock);
14,131✔
1296
        if (closed)
99,926✔
1297
            return false;
2✔
1298
        if (is_full())
99,924✔
UNCOV
1299
            not_full.notify_all();
×
1300
        e = std::move(data[reader++ % sz]);
99,924✔
1301
        return true;
99,924✔
1302
    }
99,924✔
1303

1304
    void reopen()
1305
    {
1306
        // no concurrent access allowed here
1307
        closed = false;
1308
    }
1309

1310
    void close()
1311
    {
2✔
1312
        std::unique_lock<std::mutex> lock(mutex);
2✔
1313
        closed = true;
2✔
1314
        not_empty_or_closed.notify_all();
2✔
1315
    }
2✔
1316

1317
private:
1318
    std::mutex mutex;
1319
    std::condition_variable not_full;
1320
    std::condition_variable not_empty_or_closed;
1321
    size_t reader = 0;
1322
    size_t writer = 0;
1323
    bool closed = false;
1324
    size_t sz;
1325
    std::unique_ptr<T[]> data;
1326
};
1327

1328
// Background thread for test below.
1329
void deleter_thread(ConcurrentQueue<LnkLstPtr>& queue)
1330
{
2✔
1331
    Random random(random_int<unsigned long>());
2✔
1332
    bool closed = false;
2✔
1333
    while (!closed) {
99,928✔
1334
        LnkLstPtr r;
99,926✔
1335
        // prevent the compiler from eliminating a loop:
49,984✔
1336
        volatile int delay = random.draw_int_mod(10000);
99,926✔
1337
        closed = !queue.get(r);
99,926✔
1338
        // random delay goes *after* get(), so that it comes
49,984✔
1339
        // after the potentially synchronizing locking
49,984✔
1340
        // operation inside queue.get()
49,984✔
1341
        while (delay > 0)
500,291,341✔
1342
            delay = delay - 1;
500,191,415✔
1343
        // just let 'r' die
49,984✔
1344
    }
99,926✔
1345
}
2✔
1346
} // namespace
1347

1348
TEST(LangBindHelper_ConcurrentLinkViewDeletes)
1349
{
2✔
1350
    // This tests checks concurrent deletion of LinkViews.
1✔
1351
    // It is structured as a mutator which creates and uses
1✔
1352
    // LinkView accessors, and a background deleter which
1✔
1353
    // consumes LinkViewRefs and makes them go out of scope
1✔
1354
    // concurrently with the new references being created.
1✔
1355

1✔
1356
    // Number of table entries (and hence, max number of accessors)
1✔
1357
    const int table_size = 1000;
2✔
1358

1✔
1359
    // Number of references produced (some will refer to the same
1✔
1360
    // accessor)
1✔
1361
    const int max_refs = 50000;
2✔
1362

1✔
1363
    // Frequency of references that are used to change the
1✔
1364
    // database during the test.
1✔
1365
    const int change_frequency_per_mill = 50000; // 5pct changes
2✔
1366

1✔
1367
    // Number of references that may be buffered for communication
1✔
1368
    // between main thread and deleter thread. Should be large enough
1✔
1369
    // to allow considerable overlap.
1✔
1370
    const int buffer_size = 2000;
2✔
1371

1✔
1372
    Random random(random_int<unsigned long>());
2✔
1373

1✔
1374
    // setup two tables with empty linklists inside
1✔
1375
    SHARED_GROUP_TEST_PATH(path);
2✔
1376
    ShortCircuitHistory hist;
2✔
1377
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1378

1✔
1379
    // Start a read transaction (to be repeatedly advanced)
1✔
1380
    std::vector<ObjKey> o_keys;
2✔
1381
    std::vector<ObjKey> t_keys;
2✔
1382
    ColKey ck;
2✔
1383
    auto rt = sg->start_read();
2✔
1384
    {
2✔
1385
        // setup tables with empty linklists
1✔
1386
        WriteTransaction wt(sg);
2✔
1387
        TableRef origin = wt.add_table("origin");
2✔
1388
        TableRef target = wt.add_table("target");
2✔
1389
        ck = origin->add_column_list(*target, "ll");
2✔
1390
        origin->create_objects(table_size, o_keys);
2✔
1391
        target->create_objects(table_size, t_keys);
2✔
1392
        wt.commit();
2✔
1393
    }
2✔
1394
    rt->advance_read();
2✔
1395

1✔
1396
    // Create accessors for random entries in the table.
1✔
1397
    // occasionally modify the database through the accessor.
1✔
1398
    // feed the accessor refs to the background thread for
1✔
1399
    // later deletion.
1✔
1400
    util::Thread deleter;
2✔
1401
    ConcurrentQueue<LnkLstPtr> queue(buffer_size);
2✔
1402
    deleter.start([&] {
2✔
1403
        deleter_thread(queue);
2✔
1404
    });
2✔
1405
    for (int i = 0; i < max_refs; ++i) {
100,002✔
1406
        TableRef origin = rt->get_table("origin");
100,000✔
1407
        int ndx = random.draw_int_mod(table_size);
100,000✔
1408
        Obj o = origin->get_object(o_keys[ndx]);
100,000✔
1409
        LnkLstPtr lw = o.get_linklist_ptr(ck);
100,000✔
1410
        bool will_add = change_frequency_per_mill > random.draw_int_mod(1000000);
100,000✔
1411
        if (will_add) {
100,000✔
1412
            rt->promote_to_write();
4,972✔
1413
            lw->add(t_keys[ndx]);
4,972✔
1414
            rt->commit_and_continue_as_read();
4,972✔
1415
        }
4,972✔
1416
        queue.put(lw);
100,000✔
1417
    }
100,000✔
1418
    queue.close();
2✔
1419
    deleter.join();
2✔
1420
}
2✔
1421

1422
TEST(LangBindHelper_AdvanceReadTransact_InsertLink)
1423
{
2✔
1424
    // This test checks that Table::insert_link() works across transaction
1✔
1425
    // boundaries (advance transaction).
1✔
1426

1✔
1427
    SHARED_GROUP_TEST_PATH(path);
2✔
1428
    ShortCircuitHistory hist;
2✔
1429
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1430

1✔
1431
    // Start a read transaction (to be repeatedly advanced)
1✔
1432
    TransactionRef rt = sg->start_read();
2✔
1433
    CHECK_EQUAL(0, rt->size());
2✔
1434
    ColKey col;
2✔
1435
    ObjKey target_key;
2✔
1436
    {
2✔
1437
        WriteTransaction wt(sg);
2✔
1438
        TableRef origin_w = wt.add_table("origin");
2✔
1439
        TableRef target_w = wt.add_table("target");
2✔
1440
        col = origin_w->add_column(*target_w, "");
2✔
1441
        target_w->add_column(type_Int, "");
2✔
1442
        target_key = target_w->create_object().get_key();
2✔
1443
        wt.commit();
2✔
1444
    }
2✔
1445
    rt->advance_read();
2✔
1446
    rt->verify();
2✔
1447
    ConstTableRef origin = rt->get_table("origin");
2✔
1448
    ConstTableRef target = rt->get_table("target");
2✔
1449
    {
2✔
1450
        WriteTransaction wt(sg);
2✔
1451
        TableRef origin_w = wt.get_table("origin");
2✔
1452
        auto obj = origin_w->create_object();
2✔
1453
        obj.set(col, target_key);
2✔
1454
        wt.commit();
2✔
1455
    }
2✔
1456
    rt->advance_read();
2✔
1457
    CHECK(origin);
2✔
1458
    CHECK(target);
2✔
1459
    rt->verify();
2✔
1460
}
2✔
1461

1462

1463
TEST(LangBindHelper_AdvanceReadTransact_LinkToNeighbour)
1464
{
2✔
1465
    // This test checks that you can insert a link to an object that resides
1✔
1466
    // in the same cluster as the origin object.
1✔
1467

1✔
1468
    SHARED_GROUP_TEST_PATH(path);
2✔
1469
    ShortCircuitHistory hist;
2✔
1470
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1471

1✔
1472
    // Start a read transaction (to be repeatedly advanced)
1✔
1473
    TransactionRef rt = sg->start_read();
2✔
1474
    CHECK_EQUAL(0, rt->size());
2✔
1475
    ColKey col;
2✔
1476
    std::vector<ObjKey> keys;
2✔
1477
    {
2✔
1478
        WriteTransaction wt(sg);
2✔
1479
        TableRef table = wt.add_table("table");
2✔
1480
        table->add_column(type_Int, "integers");
2✔
1481
        col = table->add_column(*table, "links");
2✔
1482
        table->create_objects(10, keys);
2✔
1483
        wt.commit();
2✔
1484
    }
2✔
1485
    rt->advance_read();
2✔
1486
    rt->verify();
2✔
1487
    {
2✔
1488
        WriteTransaction wt(sg);
2✔
1489
        TableRef table = wt.get_table("table");
2✔
1490
        table->get_object(keys[0]).set(col, keys[1]);
2✔
1491
        table->get_object(keys[1]).set(col, keys[2]);
2✔
1492
        wt.commit();
2✔
1493
    }
2✔
1494
    rt->advance_read();
2✔
1495
    rt->verify();
2✔
1496
}
2✔
1497

1498
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_RemoveTableWithColumns, testing_supports_spawn_process)
1499
{
2✔
1500
    SHARED_GROUP_TEST_PATH(path);
2✔
1501
    if (test_util::SpawnedProcess::is_parent()) {
2✔
1502
        std::unique_ptr<Replication> hist_parent(make_in_realm_history());
2✔
1503
        DBRef sg = DB::create(*hist_parent, path, DBOptions(crypt_key()));
2✔
1504

1✔
1505
        // Start a read transaction (to be repeatedly advanced)
1✔
1506
        TransactionRef rt = sg->start_read();
2✔
1507
        CHECK_EQUAL(0, rt->size());
2✔
1508
    }
2✔
1509
    auto process = test_util::spawn_process(test_context.test_details.test_name, "initial_write");
2✔
1510
    if (process->is_child()) {
2✔
1511
        {
×
1512
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1513
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1514
            WriteTransaction wt(sg_w);
×
1515
            TableRef alpha_w = wt.add_table("alpha");
×
1516
            TableRef beta_w = wt.add_table("beta");
×
1517
            TableRef gamma_w = wt.add_table("gamma");
×
1518
            TableRef delta_w = wt.add_table("delta");
×
1519
            TableRef epsilon_w = wt.add_table("epsilon");
×
1520
            alpha_w->add_column(type_Int, "alpha-1");
×
1521
            beta_w->add_column(*delta_w, "beta-1");
×
1522
            gamma_w->add_column(*gamma_w, "gamma-1");
×
1523
            delta_w->add_column(type_Int, "delta-1");
×
1524
            epsilon_w->add_column(*delta_w, "epsilon-1");
×
1525
            wt.commit();
×
1526
        } // clean up sg before exit
×
1527
        exit(0);
×
1528
    }
×
1529
    else if (process->is_parent()) {
2✔
1530
        process->wait_for_child_to_finish();
2✔
1531

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

1✔
1535
        // Start a read transaction (to be repeatedly advanced)
1✔
1536
        TransactionRef rt = sg->start_read();
2✔
1537
        rt->advance_read();
2✔
1538
        rt->verify();
2✔
1539

1✔
1540
        CHECK_EQUAL(5, rt->size());
2✔
1541
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1542
        ConstTableRef beta = rt->get_table("beta");
2✔
1543
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1544
        ConstTableRef delta = rt->get_table("delta");
2✔
1545
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1546
        CHECK(alpha);
2✔
1547
        CHECK(beta);
2✔
1548
        CHECK(gamma);
2✔
1549
        CHECK(delta);
2✔
1550
        CHECK(epsilon);
2✔
1551
    }
2✔
1552
    // Remove table with columns, but no link columns, and table is not a link
1✔
1553
    // target.
1✔
1554
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_alpha");
2✔
1555
    if (process->is_child()) {
2✔
1556
        {
×
1557
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1558
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1559
            WriteTransaction wt(sg_w);
×
1560
            wt.get_group().remove_table("alpha");
×
1561
            wt.commit();
×
1562
        }
×
1563
        exit(0);
×
1564
    }
×
1565
    else if (process->is_parent()) {
2✔
1566
        process->wait_for_child_to_finish();
2✔
1567

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

1✔
1571
        // Start a read transaction (to be repeatedly advanced)
1✔
1572
        TransactionRef rt = sg->start_read();
2✔
1573
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1574
        ConstTableRef beta = rt->get_table("beta");
2✔
1575
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1576
        ConstTableRef delta = rt->get_table("delta");
2✔
1577
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1578
        rt->advance_read();
2✔
1579
        rt->verify();
2✔
1580

1✔
1581
        CHECK_EQUAL(4, rt->size());
2✔
1582
        CHECK_NOT(alpha);
2✔
1583
        CHECK(beta);
2✔
1584
        CHECK(gamma);
2✔
1585
        CHECK(delta);
2✔
1586
        CHECK(epsilon);
2✔
1587
    }
2✔
1588
    // Remove table with link column, and table is not a link target.
1✔
1589
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_beta");
2✔
1590
    if (process->is_child()) {
2✔
1591
        {
×
1592
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1593
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1594
            WriteTransaction wt(sg_w);
×
1595
            wt.get_group().remove_table("beta");
×
1596
            wt.commit();
×
1597
        }
×
1598
        exit(0);
×
1599
    }
×
1600
    else if (process->is_parent()) {
2✔
1601
        process->wait_for_child_to_finish();
2✔
1602

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

1✔
1606
        // Start a read transaction (to be repeatedly advanced)
1✔
1607
        TransactionRef rt = sg->start_read();
2✔
1608
        rt->advance_read();
2✔
1609
        rt->verify();
2✔
1610

1✔
1611
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1612
        ConstTableRef beta = rt->get_table("beta");
2✔
1613
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1614
        ConstTableRef delta = rt->get_table("delta");
2✔
1615
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1616
        CHECK_EQUAL(3, rt->size());
2✔
1617
        CHECK_NOT(alpha);
2✔
1618
        CHECK_NOT(beta);
2✔
1619
        CHECK(gamma);
2✔
1620
        CHECK(delta);
2✔
1621
        CHECK(epsilon);
2✔
1622
    }
2✔
1623
    // Remove table with self-link column, and table is not a target of link
1✔
1624
    // columns of other tables.
1✔
1625
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_gamma");
2✔
1626
    if (process->is_child()) {
2✔
1627
        {
×
1628
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1629
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1630
            WriteTransaction wt(sg_w);
×
1631
            wt.get_group().remove_table("gamma");
×
1632
            wt.commit();
×
1633
        }
×
1634
        exit(0);
×
1635
    }
×
1636
    else if (process->is_parent()) {
2✔
1637
        process->wait_for_child_to_finish();
2✔
1638

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

1✔
1642
        // Start a read transaction (to be repeatedly advanced)
1✔
1643
        TransactionRef rt = sg->start_read();
2✔
1644
        rt->advance_read();
2✔
1645
        rt->verify();
2✔
1646

1✔
1647
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1648
        ConstTableRef beta = rt->get_table("beta");
2✔
1649
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1650
        ConstTableRef delta = rt->get_table("delta");
2✔
1651
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1652
        CHECK_EQUAL(2, rt->size());
2✔
1653
        CHECK_NOT(alpha);
2✔
1654
        CHECK_NOT(beta);
2✔
1655
        CHECK_NOT(gamma);
2✔
1656
        CHECK(delta);
2✔
1657
        CHECK(epsilon);
2✔
1658
    }
2✔
1659
    // Try, but fail to remove table which is a target of link columns of other
1✔
1660
    // tables.
1✔
1661
    process = test_util::spawn_process(test_context.test_details.test_name, "remove_delta");
2✔
1662
    if (process->is_child()) {
2✔
1663
        {
×
1664
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
1665
            DBRef sg_w = DB::create(*hist, path, DBOptions(crypt_key()));
×
1666
            WriteTransaction wt(sg_w);
×
1667
            CHECK_THROW(wt.get_group().remove_table("delta"), CrossTableLinkTarget);
×
1668
            wt.commit();
×
1669
        }
×
1670
        exit(0);
×
1671
    }
×
1672
    else if (process->is_parent()) {
2✔
1673
        process->wait_for_child_to_finish();
2✔
1674

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

1✔
1678
        // Start a read transaction (to be repeatedly advanced)
1✔
1679
        TransactionRef rt = sg->start_read();
2✔
1680
        rt->advance_read();
2✔
1681
        rt->verify();
2✔
1682
        ConstTableRef alpha = rt->get_table("alpha");
2✔
1683
        ConstTableRef beta = rt->get_table("beta");
2✔
1684
        ConstTableRef gamma = rt->get_table("gamma");
2✔
1685
        ConstTableRef delta = rt->get_table("delta");
2✔
1686
        ConstTableRef epsilon = rt->get_table("epsilon");
2✔
1687

1✔
1688
        CHECK_EQUAL(2, rt->size());
2✔
1689
        CHECK_NOT(alpha);
2✔
1690
        CHECK_NOT(beta);
2✔
1691
        CHECK_NOT(gamma);
2✔
1692
        CHECK(delta);
2✔
1693
        CHECK(epsilon);
2✔
1694
    }
2✔
1695
}
2✔
1696

1697
TEST(LangBindHelper_AdvanceReadTransact_CascadeRemove_ColumnLink)
1698
{
2✔
1699
    SHARED_GROUP_TEST_PATH(path);
2✔
1700
    ShortCircuitHistory hist;
2✔
1701
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1702

1✔
1703
    ColKey col;
2✔
1704
    {
2✔
1705
        WriteTransaction wt(sg);
2✔
1706
        auto origin = wt.add_table("origin");
2✔
1707
        auto target = wt.add_table("target", Table::Type::Embedded);
2✔
1708
        col = origin->add_column(*target, "o_1");
2✔
1709
        target->add_column(type_Int, "t_1");
2✔
1710
        wt.commit();
2✔
1711
    }
2✔
1712

1✔
1713
    // Start a read transaction (to be repeatedly advanced)
1✔
1714
    auto rt = sg->start_read();
2✔
1715
    auto target = rt->get_table("target");
2✔
1716

1✔
1717
    ObjKey target_key0, target_key1;
2✔
1718
    Obj target_obj0, target_obj1;
2✔
1719

1✔
1720
    auto perform_change = [&](util::FunctionRef<void(Table&)> func) {
6✔
1721
        // Ensure there are two rows in each table, with each row in `origin`
3✔
1722
        // pointing to the corresponding row in `target`
3✔
1723
        {
6✔
1724
            WriteTransaction wt(sg);
6✔
1725
            auto origin_w = wt.get_table("origin");
6✔
1726
            auto target_w = wt.get_table("target");
6✔
1727

3✔
1728
            origin_w->clear();
6✔
1729
            target_w->clear();
6✔
1730
            auto o0 = origin_w->create_object();
6✔
1731
            auto o1 = origin_w->create_object();
6✔
1732
            target_key0 = o0.create_and_set_linked_object(col).get_key();
6✔
1733
            target_key1 = o1.create_and_set_linked_object(col).get_key();
6✔
1734
            wt.commit();
6✔
1735
        }
6✔
1736

3✔
1737
        // Grab the row accessors before applying the modification being tested
3✔
1738
        rt->advance_read();
6✔
1739
        rt->verify();
6✔
1740
        target_obj0 = target->get_object(target_key0);
6✔
1741
        target_obj1 = target->get_object(target_key1);
6✔
1742

3✔
1743
        // Perform the modification
3✔
1744
        {
6✔
1745
            WriteTransaction wt(sg);
6✔
1746
            func(*wt.get_table("origin"));
6✔
1747
            wt.commit();
6✔
1748
        }
6✔
1749

3✔
1750
        rt->advance_read();
6✔
1751
        rt->verify();
6✔
1752
        // Leave `group` and the target accessors in a state which can be tested
3✔
1753
        // with the changes applied
3✔
1754
    };
6✔
1755

1✔
1756
    // Break link by clearing table
1✔
1757
    perform_change([](Table& origin) {
2✔
1758
        origin.clear();
2✔
1759
    });
2✔
1760
    CHECK(!target_obj0.is_valid());
2✔
1761
    CHECK(!target_obj1.is_valid());
2✔
1762
    CHECK_EQUAL(target->size(), 0);
2✔
1763

1✔
1764
    // Break link by nullifying
1✔
1765
    perform_change([&](Table& origin) {
2✔
1766
        origin.get_object(1).set_null(col);
2✔
1767
    });
2✔
1768
    CHECK(target_obj0.is_valid());
2✔
1769
    CHECK(!target_obj1.is_valid());
2✔
1770
    CHECK_EQUAL(target->size(), 1);
2✔
1771

1✔
1772
    // Break link by reassign
1✔
1773
    perform_change([&](Table& origin) {
2✔
1774
        origin.get_object(1).create_and_set_linked_object(col);
2✔
1775
    });
2✔
1776
    CHECK(target_obj0.is_valid());
2✔
1777
    CHECK(!target_obj1.is_valid());
2✔
1778
    CHECK_EQUAL(target->size(), 2);
2✔
1779
}
2✔
1780

1781

1782
TEST(LangBindHelper_AdvanceReadTransact_CascadeRemove_ColumnLinkList)
1783
{
2✔
1784
    SHARED_GROUP_TEST_PATH(path);
2✔
1785
    ShortCircuitHistory hist;
2✔
1786
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
1787

1✔
1788
    ColKey col;
2✔
1789
    {
2✔
1790
        WriteTransaction wt(sg);
2✔
1791
        auto origin = wt.add_table("origin");
2✔
1792
        auto target = wt.add_table("target", Table::Type::Embedded);
2✔
1793
        col = origin->add_column_list(*target, "o_1");
2✔
1794
        target->add_column(type_Int, "t_1");
2✔
1795
        wt.commit();
2✔
1796
    }
2✔
1797

1✔
1798
    // Start a read transaction (to be repeatedly advanced)
1✔
1799
    auto rt = sg->start_read();
2✔
1800
    auto target = rt->get_table("target");
2✔
1801

1✔
1802
    ObjKey target_key0, target_key1;
2✔
1803
    Obj target_obj0, target_obj1;
2✔
1804

1✔
1805
    auto perform_change = [&](util::FunctionRef<void(Table&)> func) {
8✔
1806
        // Ensure there are two rows in each table, with each row in `origin`
4✔
1807
        // pointing to the corresponding row in `target`
4✔
1808
        {
8✔
1809
            WriteTransaction wt(sg);
8✔
1810
            auto origin_w = wt.get_table("origin");
8✔
1811
            auto target_w = wt.get_table("target");
8✔
1812

4✔
1813
            origin_w->clear();
8✔
1814
            target_w->clear();
8✔
1815
            auto o0 = origin_w->create_object();
8✔
1816
            auto o1 = origin_w->create_object();
8✔
1817
            target_key0 = o0.get_linklist(col).create_and_insert_linked_object(0).get_key();
8✔
1818
            target_key1 = o1.get_linklist(col).create_and_insert_linked_object(0).get_key();
8✔
1819
            wt.commit();
8✔
1820
        }
8✔
1821

4✔
1822
        // Grab the row accessors before applying the modification being tested
4✔
1823
        rt->advance_read();
8✔
1824
        rt->verify();
8✔
1825
        target_obj0 = target->get_object(target_key0);
8✔
1826
        target_obj1 = target->get_object(target_key1);
8✔
1827

4✔
1828
        // Perform the modification
4✔
1829
        {
8✔
1830
            WriteTransaction wt(sg);
8✔
1831
            func(*wt.get_table("origin"));
8✔
1832
            wt.commit();
8✔
1833
        }
8✔
1834

4✔
1835
        rt->advance_read();
8✔
1836
        rt->verify();
8✔
1837
        // Leave `group` and the target accessors in a state which can be tested
4✔
1838
        // with the changes applied
4✔
1839
    };
8✔
1840

1✔
1841

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

1✔
1849
    // Break link by removal from list
1✔
1850
    perform_change([&](Table& origin) {
2✔
1851
        origin.get_object(1).get_linklist(col).remove(0);
2✔
1852
    });
2✔
1853
    CHECK(target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1854
    CHECK_EQUAL(target->size(), 1);
2✔
1855

1✔
1856
    // Break link by reassign
1✔
1857
    perform_change([&](Table& origin) {
2✔
1858
        origin.get_object(1).get_linklist(col).create_and_set_linked_object(0);
2✔
1859
    });
2✔
1860
    CHECK(target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1861
    CHECK_EQUAL(target->size(), 2);
2✔
1862

1✔
1863
    // Break link by clearing table
1✔
1864
    perform_change([](Table& origin) {
2✔
1865
        origin.clear();
2✔
1866
    });
2✔
1867
    CHECK(!target_obj0.is_valid() && !target_obj1.is_valid());
2✔
1868
    CHECK_EQUAL(target->size(), 0);
2✔
1869
}
2✔
1870

1871

1872
TEST(LangBindHelper_AdvanceReadTransact_IntIndex)
1873
{
2✔
1874
    SHARED_GROUP_TEST_PATH(path);
2✔
1875

1✔
1876
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1877
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1878
    auto g = sg->start_read();
2✔
1879
    g->promote_to_write();
2✔
1880

1✔
1881
    TableRef target = g->add_table("target");
2✔
1882
    auto col = target->add_column(type_Int, "pk");
2✔
1883
    target->add_search_index(col);
2✔
1884

1✔
1885
    std::vector<ObjKey> obj_keys;
2✔
1886
    target->create_objects(REALM_MAX_BPNODE_SIZE + 1, obj_keys);
2✔
1887

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

1✔
1890
    // open a second copy that'll be advanced over the write
1✔
1891
    auto g_r = sg->start_read();
2✔
1892
    TableRef t_r = g_r->get_table("target");
2✔
1893

1✔
1894
    g->promote_to_write();
2✔
1895

1✔
1896
    // Ensure that the index has a different bptree layout so that failing to
1✔
1897
    // refresh it will do bad things
1✔
1898
    int i = 0;
2✔
1899
    for (auto it = target->begin(); it != target->end(); ++it)
2,004✔
1900
        it->set(col, i++);
2,002✔
1901

1✔
1902
    g->commit_and_continue_as_read();
2✔
1903

1✔
1904
    g_r->promote_to_write();
2✔
1905
    // Crashes if index has an invalid parent ref
1✔
1906
    t_r->clear();
2✔
1907
}
2✔
1908

1909
NONCONCURRENT_TEST_IF(LangBindHelper_AdvanceReadTransact_TableClear, testing_supports_spawn_process)
1910
{
2✔
1911
    SHARED_GROUP_TEST_PATH(path);
2✔
1912

1✔
1913
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1914
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1915
    ColKey col;
2✔
1916
    if (SpawnedProcess::is_parent()) {
2✔
1917
        WriteTransaction wt(sg);
2✔
1918
        TableRef table = wt.add_table("table");
2✔
1919
        col = table->add_column(type_Int, "col");
2✔
1920
        table->create_object();
2✔
1921
        wt.commit();
2✔
1922
    }
2✔
1923

1✔
1924
    auto reader = sg->start_read();
2✔
1925
    auto table = reader->get_table("table");
2✔
1926
    TableView tv = table->where().find_all();
2✔
1927
    auto obj = *table->begin();
2✔
1928
    CHECK(obj.is_valid());
2✔
1929

1✔
1930
    auto process = test_util::spawn_process(test_context.test_details.test_name, "external_clear");
2✔
1931
    if (process->is_child()) {
2✔
1932
        {
×
1933
            std::unique_ptr<Replication> hist_w(make_in_realm_history());
×
1934
            DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
×
1935
            WriteTransaction wt(sg_w);
×
1936
            wt.get_table("table")->clear();
×
1937
            wt.commit();
×
1938
        }
×
1939
        exit(0);
×
1940
    }
×
1941
    else if (process->is_parent()) {
2✔
1942
        process->wait_for_child_to_finish();
2✔
1943

1✔
1944
        reader->advance_read();
2✔
1945

1✔
1946
        CHECK(!obj.is_valid());
2✔
1947

1✔
1948
        CHECK_EQUAL(tv.size(), 1);
2✔
1949
        CHECK(!tv.is_in_sync());
2✔
1950
        // key is still there...
1✔
1951
        CHECK(tv.get_key(0));
2✔
1952
        // but no obj for that key...
1✔
1953
        CHECK_NOT(tv.get_object(0).is_valid());
2✔
1954

1✔
1955
        tv.sync_if_needed();
2✔
1956
        CHECK_EQUAL(tv.size(), 0);
2✔
1957
    }
2✔
1958
}
2✔
1959

1960
TEST(LangBindHelper_AdvanceReadTransact_UnorderedTableViewClear)
1961
{
2✔
1962
    SHARED_GROUP_TEST_PATH(path);
2✔
1963

1✔
1964
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
1965
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
1966
    ObjKey first_obj, last_obj;
2✔
1967
    ColKey col;
2✔
1968
    {
2✔
1969
        WriteTransaction wt(sg);
2✔
1970
        TableRef table = wt.add_table("table");
2✔
1971
        col = table->add_column(type_Int, "col");
2✔
1972
        first_obj = table->create_object().set_all(0).get_key();
2✔
1973
        table->create_object().set_all(1);
2✔
1974
        last_obj = table->create_object().set_all(2).get_key();
2✔
1975
        wt.commit();
2✔
1976
    }
2✔
1977

1✔
1978
    auto reader = sg->start_read();
2✔
1979
    auto table = reader->get_table("table");
2✔
1980
    auto obj = table->get_object(last_obj);
2✔
1981
    CHECK_EQUAL(obj.get<int64_t>(col), 2);
2✔
1982

1✔
1983
    {
2✔
1984
        // Remove the first row via unordered removal, resulting in the '2' row
1✔
1985
        // moving to index 0 (with ordered removal it would instead move to index 1)
1✔
1986
        WriteTransaction wt(sg);
2✔
1987
        wt.get_table("table")->where().equal(col, 0).find_all().clear();
2✔
1988
        wt.commit();
2✔
1989
    }
2✔
1990

1✔
1991
    reader->advance_read();
2✔
1992

1✔
1993
    CHECK(obj.is_valid());
2✔
1994
    CHECK_EQUAL(obj.get<int64_t>(col), 2);
2✔
1995
}
2✔
1996

1997
namespace {
1998
// A base class for transaction log parsers so that tests which want to test
1999
// just a single part of the transaction log handling don't have to implement
2000
// the entire interface
2001
class NoOpTransactionLogParser {
2002
public:
2003
    NoOpTransactionLogParser(TestContext& context)
2004
        : test_context(context)
2005
    {
22✔
2006
    }
22✔
2007

2008
    TableKey get_current_table() const
2009
    {
8✔
2010
        return m_current_table;
8✔
2011
    }
8✔
2012

2013
    std::pair<ColKey, ObjKey> get_current_linkview() const
2014
    {
×
2015
        return {m_current_linkview_col, m_current_linkview_row};
×
2016
    }
×
2017

2018
protected:
2019
    TestContext& test_context;
2020

2021
private:
2022
    TableKey m_current_table;
2023
    ColKey m_current_linkview_col;
2024
    ObjKey m_current_linkview_row;
2025

2026
public:
2027
    void parse_complete() {}
12✔
2028

2029
    bool select_table(TableKey t)
2030
    {
30✔
2031
        m_current_table = t;
30✔
2032
        return true;
30✔
2033
    }
30✔
2034

2035
    bool select_collection(ColKey col_key, ObjKey obj_key)
2036
    {
4✔
2037
        m_current_linkview_col = col_key;
4✔
2038
        m_current_linkview_row = obj_key;
4✔
2039
        return true;
4✔
2040
    }
4✔
2041

2042
    // Default no-op implementations of all of the mutation instructions
2043
    bool insert_group_level_table(TableKey)
2044
    {
×
2045
        return false;
×
2046
    }
×
2047
    bool erase_class(TableKey)
2048
    {
×
2049
        return false;
×
2050
    }
×
2051
    bool rename_class(TableKey)
2052
    {
×
2053
        return false;
×
2054
    }
×
2055
    bool insert_column(ColKey)
2056
    {
×
2057
        return false;
×
2058
    }
×
2059
    bool erase_column(ColKey)
2060
    {
×
2061
        return false;
×
2062
    }
×
2063
    bool rename_column(ColKey)
2064
    {
×
2065
        return false;
×
2066
    }
×
2067
    bool set_link_type(ColKey)
2068
    {
×
2069
        return false;
×
2070
    }
×
2071
    bool create_object(ObjKey)
2072
    {
×
2073
        return false;
×
2074
    }
×
2075
    bool remove_object(ObjKey)
2076
    {
×
2077
        return false;
×
2078
    }
×
2079
    bool collection_set(size_t)
2080
    {
×
2081
        return false;
×
2082
    }
×
2083
    bool collection_clear(size_t)
2084
    {
×
2085
        return false;
×
2086
    }
×
2087
    bool collection_erase(size_t)
2088
    {
×
2089
        return false;
×
2090
    }
×
2091
    bool collection_insert(size_t)
2092
    {
×
2093
        return false;
×
2094
    }
×
2095
    bool collection_move(size_t, size_t)
2096
    {
×
2097
        return false;
×
2098
    }
×
2099
    bool modify_object(ColKey, ObjKey)
2100
    {
×
2101
        return false;
×
2102
    }
×
2103
    bool typed_link_change(ColKey, TableKey)
2104
    {
×
2105
        return true;
×
2106
    }
×
2107
};
2108

2109
struct AdvanceReadTransact {
2110
    template <typename Func>
2111
    static void call(TransactionRef tr, Func* func)
2112
    {
10✔
2113
        tr->advance_read(func);
10✔
2114
    }
10✔
2115
};
2116

2117
struct PromoteThenRollback {
2118
    template <typename Func>
2119
    static void call(TransactionRef tr, Func* func)
2120
    {
10✔
2121
        tr->promote_to_write(func);
10✔
2122
        tr->rollback_and_continue_as_read();
10✔
2123
    }
10✔
2124
};
2125

2126
} // unnamed namespace
2127

2128
TEST_TYPES(LangBindHelper_AdvanceReadTransact_TransactLog, AdvanceReadTransact, PromoteThenRollback)
2129
{
4✔
2130
    SHARED_GROUP_TEST_PATH(path);
4✔
2131
    std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
2132
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
2133
    ColKey c0, c1;
4✔
2134
    {
4✔
2135
        WriteTransaction wt(sg);
4✔
2136
        c0 = wt.add_table("table 1")->add_column(type_Int, "int");
4✔
2137
        c1 = wt.add_table("table 2")->add_column(type_Int, "int");
4✔
2138
        wt.commit();
4✔
2139
    }
4✔
2140

2✔
2141
    auto tr = sg->start_read();
4✔
2142

2✔
2143
    {
4✔
2144
        // With no changes, the handler should not be called at all
2✔
2145
        struct : NoOpTransactionLogParser {
4✔
2146
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2147
            void parse_complete()
4✔
2148
            {
2✔
2149
                CHECK(false);
×
2150
            }
×
2151
        } parser(test_context);
4✔
2152
        TEST_TYPE::call(tr, &parser);
4✔
2153
    }
4✔
2154

2✔
2155
    {
4✔
2156
        // With an empty change, parse_complete() and nothing else should be called
2✔
2157
        auto wt = sg->start_write();
4✔
2158
        wt->commit();
4✔
2159

2✔
2160
        struct foo : NoOpTransactionLogParser {
4✔
2161
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2162

2✔
2163
            bool called = false;
4✔
2164
            void parse_complete()
4✔
2165
            {
4✔
2166
                called = true;
4✔
2167
            }
4✔
2168
        } parser(test_context);
4✔
2169
        TEST_TYPE::call(tr, &parser);
4✔
2170
        CHECK(parser.called);
4✔
2171
    }
4✔
2172
    ObjKey o0, o1;
4✔
2173
    {
4✔
2174
        // Make a simple modification and verify that the appropriate handler is called
2✔
2175
        struct foo : NoOpTransactionLogParser {
4✔
2176
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2177

2✔
2178
            size_t expected_table = 0;
4✔
2179
            TableKey t1;
4✔
2180
            TableKey t2;
4✔
2181

2✔
2182
            bool create_object(ObjKey)
4✔
2183
            {
8✔
2184
                CHECK_EQUAL(expected_table ? t2 : t1, get_current_table());
8✔
2185
                ++expected_table;
8✔
2186

4✔
2187
                return true;
8✔
2188
            }
8✔
2189
        } parser(test_context);
4✔
2190

2✔
2191
        WriteTransaction wt(sg);
4✔
2192
        parser.t1 = wt.get_table("table 1")->get_key();
4✔
2193
        parser.t2 = wt.get_table("table 2")->get_key();
4✔
2194
        o0 = wt.get_table("table 1")->create_object().get_key();
4✔
2195
        o1 = wt.get_table("table 2")->create_object().get_key();
4✔
2196
        wt.commit();
4✔
2197

2✔
2198
        TEST_TYPE::call(tr, &parser);
4✔
2199
        CHECK_EQUAL(2, parser.expected_table);
4✔
2200
    }
4✔
2201
    ColKey c2, c3;
4✔
2202
    ObjKey okey;
4✔
2203
    {
4✔
2204
        // Add a table with some links
2✔
2205
        WriteTransaction wt(sg);
4✔
2206
        TableRef table = wt.add_table("link origin");
4✔
2207
        c2 = table->add_column(*wt.get_table("table 1"), "link");
4✔
2208
        c3 = table->add_column_list(*wt.get_table("table 2"), "linklist");
4✔
2209
        Obj o = table->create_object();
4✔
2210
        o.set(c2, o.get_key());
4✔
2211
        o.get_linklist(c3).add(o.get_key());
4✔
2212
        okey = o.get_key();
4✔
2213
        wt.commit();
4✔
2214

2✔
2215
        tr->advance_read();
4✔
2216
    }
4✔
2217
    {
4✔
2218
        // Verify that deleting the targets of the links logs link nullifications
2✔
2219
        WriteTransaction wt(sg);
4✔
2220
        wt.get_table("table 1")->remove_object(o0);
4✔
2221
        wt.get_table("table 2")->remove_object(o1);
4✔
2222
        wt.commit();
4✔
2223

2✔
2224
        struct : NoOpTransactionLogParser {
4✔
2225
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2226

2✔
2227
            bool remove_object(ObjKey o)
4✔
2228
            {
8✔
2229
                CHECK(o == o1 || o == o0);
8!
2230
                return true;
8✔
2231
            }
8✔
2232
            bool select_collection(ColKey col, ObjKey o)
4✔
2233
            {
4✔
2234
                CHECK(col == link_list_col);
4✔
2235
                CHECK(o == okey);
4✔
2236
                return true;
4✔
2237
            }
4✔
2238
            bool collection_erase(size_t ndx)
4✔
2239
            {
4✔
2240
                CHECK(ndx == 0);
4✔
2241
                return true;
4✔
2242
            }
4✔
2243

2✔
2244
            bool modify_object(ColKey col, ObjKey obj)
4✔
2245
            {
4✔
2246
                CHECK(col == link_col && obj == okey);
4✔
2247
                return true;
4✔
2248
            }
4✔
2249
            ObjKey o0, o1, okey;
4✔
2250
            ColKey link_col, link_list_col;
4✔
2251
        } parser(test_context);
4✔
2252
        parser.o1 = o1;
4✔
2253
        parser.o0 = o0;
4✔
2254
        parser.okey = okey;
4✔
2255
        parser.link_col = c2;
4✔
2256
        parser.link_list_col = c3;
4✔
2257
        TEST_TYPE::call(tr, &parser);
4✔
2258
    }
4✔
2259
    {
4✔
2260
        // Verify that clear() logs the correct rows
2✔
2261
        WriteTransaction wt(sg);
4✔
2262
        std::vector<ObjKey> keys;
4✔
2263
        wt.get_table("table 2")->create_objects(10, keys);
4✔
2264

2✔
2265
        auto lv = wt.get_table("link origin")->begin()->get_linklist(c3);
4✔
2266
        lv.add(keys[1]);
4✔
2267
        lv.add(keys[3]);
4✔
2268
        lv.add(keys[5]);
4✔
2269

2✔
2270
        wt.commit();
4✔
2271
        tr->advance_read();
4✔
2272
    }
4✔
2273
    {
4✔
2274
        WriteTransaction wt(sg);
4✔
2275
        wt.get_table("link origin")->begin()->get_linklist(c3).clear();
4✔
2276
        wt.commit();
4✔
2277
        struct : NoOpTransactionLogParser {
4✔
2278
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
4✔
2279

2✔
2280
            bool collection_clear(size_t old_size) const
4✔
2281
            {
4✔
2282
                CHECK_EQUAL(3, old_size);
4✔
2283
                return true;
4✔
2284
            }
4✔
2285
        } parser(test_context);
4✔
2286
        TEST_TYPE::call(tr, &parser);
4✔
2287
    }
4✔
2288
}
4✔
2289

2290

2291
TEST(LangBindHelper_AdvanceReadTransact_ErrorInObserver)
2292
{
2✔
2293
    SHARED_GROUP_TEST_PATH(path);
2✔
2294
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2295
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2296
    ColKey col;
2✔
2297
    Obj obj;
2✔
2298
    // Add some initial data and then begin a read transaction at that version
1✔
2299
    auto wt1 = sg->start_write();
2✔
2300
    TableRef table = wt1->add_table("Table");
2✔
2301
    col = table->add_column(type_Int, "int");
2✔
2302
    auto obj2 = table->create_object().set_all(10);
2✔
2303
    wt1->commit_and_continue_as_read();
2✔
2304

1✔
2305
    auto g = sg->start_read();     // must follow commit, to see table just created
2✔
2306
    obj = g->import_copy_of(obj2); // cannot be imported if table does not exist
2✔
2307
    wt1->end_read();               // wt1 must live long enough to support import_copy_of of obj2
2✔
2308
    // Modify the data with a different SG so that we can determine which version
1✔
2309
    // the read transaction is using
1✔
2310
    {
2✔
2311
        auto wt = sg->start_write();
2✔
2312
        Obj o2 = wt->import_copy_of(obj);
2✔
2313
        o2.set<int64_t>(col, 20);
2✔
2314
        wt->commit();
2✔
2315
    }
2✔
2316

1✔
2317
    struct ObserverError {
2✔
2318
    };
2✔
2319
    try {
2✔
2320
        struct : NoOpTransactionLogParser {
2✔
2321
            using NoOpTransactionLogParser::NoOpTransactionLogParser;
2✔
2322

1✔
2323
            bool modify_object(ColKey, ObjKey) const
2✔
2324
            {
2✔
2325
                throw ObserverError();
2✔
2326
            }
2✔
2327
        } parser(test_context);
2✔
2328
        g->advance_read(&parser);
2✔
2329
        CHECK(false); // Should not be reached
2✔
2330
    }
2✔
2331
    catch (ObserverError) {
2✔
2332
    }
2✔
2333

1✔
2334
    // Should still see data from old version
1✔
2335
    auto o = g->import_copy_of(obj);
2✔
2336
    CHECK_EQUAL(10, o.get<int64_t>(col));
2✔
2337

1✔
2338
    // Should be able to advance to the new version still
1✔
2339
    g->advance_read();
2✔
2340

1✔
2341
    // And see that version's data
1✔
2342
    CHECK_EQUAL(20, o.get<int64_t>(col));
2✔
2343
}
2✔
2344

2345

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

2399

2400
TEST(LangBindHelper_RollbackAndContinueAsRead)
2401
{
2✔
2402
    SHARED_GROUP_TEST_PATH(path);
2✔
2403
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2404
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2405
    {
2✔
2406
        ObjKey key;
2✔
2407
        ColKey col;
2✔
2408
        auto group = sg->start_read();
2✔
2409
        {
2✔
2410
            group->promote_to_write();
2✔
2411
            TableRef origin = group->get_or_add_table("origin");
2✔
2412
            col = origin->add_column(type_Int, "");
2✔
2413
            key = origin->create_object().set_all(42).get_key();
2✔
2414
            group->commit_and_continue_as_read();
2✔
2415
        }
2✔
2416
        group->verify();
2✔
2417
        {
2✔
2418
            // rollback of group level table insertion
1✔
2419
            group->promote_to_write();
2✔
2420
            group->get_or_add_table("nullermand");
2✔
2421
            TableRef o2 = group->get_table("nullermand");
2✔
2422
            REALM_ASSERT(o2);
2✔
2423
            group->rollback_and_continue_as_read();
2✔
2424
            TableRef o3 = group->get_table("nullermand");
2✔
2425
            REALM_ASSERT(!o3);
2✔
2426
            REALM_ASSERT(!o2);
2✔
2427
        }
2✔
2428

1✔
2429
        TableRef origin = group->get_table("origin");
2✔
2430
        Obj row = origin->get_object(key);
2✔
2431
        CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2432

1✔
2433
        {
2✔
2434
            group->promote_to_write();
2✔
2435
            auto row2 = origin->create_object().set_all(5746);
2✔
2436
            CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2437
            CHECK_EQUAL(5746, row2.get<int64_t>(col));
2✔
2438
            CHECK_EQUAL(2, origin->size());
2✔
2439
            group->verify();
2✔
2440
            group->rollback_and_continue_as_read();
2✔
2441
        }
2✔
2442
        CHECK_EQUAL(1, origin->size());
2✔
2443
        group->verify();
2✔
2444
        CHECK_EQUAL(42, row.get<int64_t>(col));
2✔
2445
        Obj row2;
2✔
2446
        {
2✔
2447
            group->promote_to_write();
2✔
2448
            row2 = origin->create_object().set_all(42);
2✔
2449
            group->commit_and_continue_as_read();
2✔
2450
        }
2✔
2451
        CHECK_EQUAL(2, origin->size());
2✔
2452
        group->verify();
2✔
2453
        CHECK_EQUAL(42, row2.get<int64_t>(col));
2✔
2454
        group->end_read();
2✔
2455
    }
2✔
2456
}
2✔
2457

2458

2459
TEST(LangBindHelper_RollbackAndContinueAsReadGroupLevelTableRemoval)
2460
{
2✔
2461
    SHARED_GROUP_TEST_PATH(path);
2✔
2462
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2463
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2464
    auto reader = sg->start_read();
2✔
2465
    {
2✔
2466
        reader->promote_to_write();
2✔
2467
        reader->get_or_add_table("a_table");
2✔
2468
        reader->commit_and_continue_as_read();
2✔
2469
    }
2✔
2470
    reader->verify();
2✔
2471
    {
2✔
2472
        // rollback of group level table delete
1✔
2473
        reader->promote_to_write();
2✔
2474
        TableRef o2 = reader->get_table("a_table");
2✔
2475
        REALM_ASSERT(o2);
2✔
2476
        reader->remove_table("a_table");
2✔
2477
        TableRef o3 = reader->get_table("a_table");
2✔
2478
        REALM_ASSERT(!o3);
2✔
2479
        reader->rollback_and_continue_as_read();
2✔
2480
        TableRef o4 = reader->get_table("a_table");
2✔
2481
        REALM_ASSERT(o4);
2✔
2482
    }
2✔
2483
    reader->verify();
2✔
2484
}
2✔
2485

2486
TEST(LangBindHelper_RollbackCircularReferenceRemoval)
2487
{
2✔
2488
    SHARED_GROUP_TEST_PATH(path);
2✔
2489
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2490
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2491
    ColKey ca, cb;
2✔
2492
    auto group = sg->start_read();
2✔
2493
    {
2✔
2494
        group->promote_to_write();
2✔
2495
        TableRef alpha = group->get_or_add_table("alpha");
2✔
2496
        TableRef beta = group->get_or_add_table("beta");
2✔
2497
        ca = alpha->add_column(*beta, "beta-1");
2✔
2498
        cb = beta->add_column(*alpha, "alpha-1");
2✔
2499
        group->commit_and_continue_as_read();
2✔
2500
    }
2✔
2501
    group->verify();
2✔
2502
    {
2✔
2503
        group->promote_to_write();
2✔
2504
        CHECK_EQUAL(2, group->size());
2✔
2505
        TableRef alpha = group->get_table("alpha");
2✔
2506
        TableRef beta = group->get_table("beta");
2✔
2507

1✔
2508
        CHECK_THROW(group->remove_table("alpha"), CrossTableLinkTarget);
2✔
2509
        beta->remove_column(cb);
2✔
2510
        alpha->remove_column(ca);
2✔
2511
        group->remove_table("beta");
2✔
2512
        CHECK_NOT(group->has_table("beta"));
2✔
2513

1✔
2514
        // Version 1: This crashes
1✔
2515
        group->rollback_and_continue_as_read();
2✔
2516
        CHECK_EQUAL(2, group->size());
2✔
2517

1✔
2518
        //        // Version 2: This works
1✔
2519
        //        LangBindHelper::commit_and_continue_as_read(sg);
1✔
2520
        //        CHECK_EQUAL(1, group->size());
1✔
2521
    }
2✔
2522
    group->verify();
2✔
2523
}
2✔
2524

2525

2526
TEST(LangBindHelper_RollbackAndContinueAsReadColumnAdd)
2527
{
2✔
2528
    SHARED_GROUP_TEST_PATH(path);
2✔
2529
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2530
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2531
    auto group = sg->start_read();
2✔
2532
    TableRef t;
2✔
2533
    {
2✔
2534
        group->promote_to_write();
2✔
2535
        t = group->get_or_add_table("a_table");
2✔
2536
        t->add_column(type_Int, "lorelei");
2✔
2537
        t->create_object().set_all(43);
2✔
2538
        CHECK_EQUAL(1, t->get_column_count());
2✔
2539
        group->commit_and_continue_as_read();
2✔
2540
    }
2✔
2541
    group->verify();
2✔
2542
    {
2✔
2543
        // add a column and regret it again
1✔
2544
        group->promote_to_write();
2✔
2545
        auto col = t->add_column(type_Int, "riget");
2✔
2546
        t->begin()->set(col, 44);
2✔
2547
        CHECK_EQUAL(2, t->get_column_count());
2✔
2548
        group->verify();
2✔
2549
        group->rollback_and_continue_as_read();
2✔
2550
        group->verify();
2✔
2551
        CHECK_EQUAL(1, t->get_column_count());
2✔
2552
    }
2✔
2553
    group->verify();
2✔
2554
}
2✔
2555

2556

2557
// This issue was uncovered while looking into the RollbackCircularReferenceRemoval issue
2558
TEST(LangBindHelper_TableLinkingRemovalIssue)
2559
{
2✔
2560
    SHARED_GROUP_TEST_PATH(path);
2✔
2561
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2562
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2563
    auto group = sg->start_read();
2✔
2564
    {
2✔
2565
        group->promote_to_write();
2✔
2566
        TableRef t1 = group->get_or_add_table("t1");
2✔
2567
        TableRef t2 = group->get_or_add_table("t2");
2✔
2568
        TableRef t3 = group->get_or_add_table("t3");
2✔
2569
        TableRef t4 = group->get_or_add_table("t4");
2✔
2570
        t1->add_column(*t2, "l12");
2✔
2571
        t2->add_column(*t3, "l23");
2✔
2572
        t3->add_column(*t4, "l34");
2✔
2573
        group->commit_and_continue_as_read();
2✔
2574
    }
2✔
2575
    group->verify();
2✔
2576
    {
2✔
2577
        group->promote_to_write();
2✔
2578
        CHECK_EQUAL(4, group->size());
2✔
2579

1✔
2580
        group->remove_table("t1");
2✔
2581
        group->remove_table("t2");
2✔
2582
        group->remove_table("t3"); // CRASHES HERE
2✔
2583
        group->remove_table("t4");
2✔
2584

1✔
2585
        group->rollback_and_continue_as_read();
2✔
2586
        CHECK_EQUAL(4, group->size());
2✔
2587
    }
2✔
2588
    group->verify();
2✔
2589
}
2✔
2590

2591

2592
// This issue was uncovered while looking into the RollbackCircularReferenceRemoval issue
2593
TEST(LangBindHelper_RollbackTableRemove)
2594
{
2✔
2595
    SHARED_GROUP_TEST_PATH(path);
2✔
2596
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2597
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2598
    auto group = sg->start_read();
2✔
2599
    {
2✔
2600
        group->promote_to_write();
2✔
2601
        TableRef alpha = group->get_or_add_table("alpha");
2✔
2602
        TableRef beta = group->get_or_add_table("beta");
2✔
2603
        beta->add_column(*alpha, "alpha-1");
2✔
2604
        group->commit_and_continue_as_read();
2✔
2605
    }
2✔
2606
    group->verify();
2✔
2607
    {
2✔
2608
        group->promote_to_write();
2✔
2609
        CHECK_EQUAL(2, group->size());
2✔
2610
        TableRef alpha = group->get_table("alpha");
2✔
2611
        TableRef beta = group->get_table("beta");
2✔
2612
        CHECK(alpha);
2✔
2613
        CHECK(beta);
2✔
2614
        group->remove_table("beta");
2✔
2615
        CHECK_NOT(group->has_table("beta"));
2✔
2616
        group->rollback_and_continue_as_read();
2✔
2617
        CHECK_EQUAL(2, group->size());
2✔
2618
    }
2✔
2619
    group->verify();
2✔
2620
}
2✔
2621

2622
TEST(LangBindHelper_RollbackTableRemove2)
2623
{
2✔
2624
    SHARED_GROUP_TEST_PATH(path);
2✔
2625
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2626
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2627
    auto group = sg->start_read();
2✔
2628
    {
2✔
2629
        group->promote_to_write();
2✔
2630
        TableRef a = group->get_or_add_table("a");
2✔
2631
        TableRef b = group->get_or_add_table("b");
2✔
2632
        TableRef c = group->get_or_add_table("c");
2✔
2633
        TableRef d = group->get_or_add_table("d");
2✔
2634
        c->add_column(*a, "a");
2✔
2635
        d->add_column(*b, "b");
2✔
2636
        group->commit_and_continue_as_read();
2✔
2637
    }
2✔
2638
    group->verify();
2✔
2639
    {
2✔
2640
        group->promote_to_write();
2✔
2641
        CHECK_EQUAL(4, group->size());
2✔
2642
        group->remove_table("c");
2✔
2643
        CHECK_NOT(group->has_table("c"));
2✔
2644
        group->verify();
2✔
2645
        group->rollback_and_continue_as_read();
2✔
2646
        CHECK_EQUAL(4, group->size());
2✔
2647
    }
2✔
2648
    group->verify();
2✔
2649
}
2✔
2650

2651
TEST(LangBindHelper_ContinuousTransactions_RollbackTableRemoval)
2652
{
2✔
2653
    // Test that it is possible to modify a table, then remove it from the
1✔
2654
    // group, and then rollback the transaction.
1✔
2655

1✔
2656
    // This triggered a bug in the instruction reverser which would incorrectly
1✔
2657
    // associate the table removal instruction with the table selection
1✔
2658
    // instruction induced by the modification, causing the latter to occur in
1✔
2659
    // the reverse log at a point where the selected table does not yet
1✔
2660
    // exist. The filler table is there to avoid an early-out in
1✔
2661
    // Group::TransactAdvancer::select_table() due to a misinterpretation of the
1✔
2662
    // reason for the missing table accessor entry.
1✔
2663

1✔
2664
    SHARED_GROUP_TEST_PATH(path);
2✔
2665
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2666
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2667
    auto group = sg->start_read();
2✔
2668
    group->promote_to_write();
2✔
2669
    group->get_or_add_table("filler");
2✔
2670
    TableRef table = group->get_or_add_table("table");
2✔
2671
    auto col = table->add_column(type_Int, "i");
2✔
2672
    Obj o = table->create_object();
2✔
2673
    group->commit_and_continue_as_read();
2✔
2674
    group->promote_to_write();
2✔
2675
    o.set<int>(col, 0);
2✔
2676
    group->remove_table("table");
2✔
2677
    group->rollback_and_continue_as_read();
2✔
2678
}
2✔
2679

2680
TEST(LangBindHelper_RollbackAndContinueAsReadLinkColumnRemove)
2681
{
2✔
2682
    SHARED_GROUP_TEST_PATH(path);
2✔
2683
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2684
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2685
    auto group = sg->start_read();
2✔
2686
    TableRef t, t2;
2✔
2687
    ColKey col;
2✔
2688
    {
2✔
2689
        // add a column
1✔
2690
        group->promote_to_write();
2✔
2691
        t = group->get_or_add_table("a_table");
2✔
2692
        t2 = group->get_or_add_table("b_table");
2✔
2693
        col = t->add_column(*t2, "bruno");
2✔
2694
        CHECK_EQUAL(1, t->get_column_count());
2✔
2695
        group->commit_and_continue_as_read();
2✔
2696
    }
2✔
2697
    group->verify();
2✔
2698
    {
2✔
2699
        // ... but then regret it
1✔
2700
        group->promote_to_write();
2✔
2701
        t->remove_column(col);
2✔
2702
        CHECK_EQUAL(0, t->get_column_count());
2✔
2703
        group->rollback_and_continue_as_read();
2✔
2704
    }
2✔
2705
}
2✔
2706

2707

2708
TEST(LangBindHelper_RollbackAndContinueAsReadColumnRemove)
2709
{
2✔
2710
    SHARED_GROUP_TEST_PATH(path);
2✔
2711
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2712
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2713
    auto group = sg->start_read();
2✔
2714
    TableRef t;
2✔
2715
    ColKey col;
2✔
2716
    {
2✔
2717
        group->promote_to_write();
2✔
2718
        t = group->get_or_add_table("a_table");
2✔
2719
        col = t->add_column(type_Int, "lorelei");
2✔
2720
        t->add_column(type_Int, "riget");
2✔
2721
        t->create_object().set_all(43, 44);
2✔
2722
        CHECK_EQUAL(2, t->get_column_count());
2✔
2723
        group->commit_and_continue_as_read();
2✔
2724
    }
2✔
2725
    group->verify();
2✔
2726
    {
2✔
2727
        // remove a column but regret it
1✔
2728
        group->promote_to_write();
2✔
2729
        CHECK_EQUAL(2, t->get_column_count());
2✔
2730
        t->remove_column(col);
2✔
2731
        group->verify();
2✔
2732
        group->rollback_and_continue_as_read();
2✔
2733
        group->verify();
2✔
2734
        CHECK_EQUAL(2, t->get_column_count());
2✔
2735
    }
2✔
2736
    group->verify();
2✔
2737
}
2✔
2738

2739

2740
TEST(LangBindHelper_RollbackAndContinueAsReadLinkList)
2741
{
2✔
2742
    SHARED_GROUP_TEST_PATH(path);
2✔
2743
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2744
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2745
    auto group = sg->start_read();
2✔
2746
    group->promote_to_write();
2✔
2747
    TableRef origin = group->add_table("origin");
2✔
2748
    TableRef target = group->add_table("target");
2✔
2749
    auto col0 = origin->add_column_list(*target, "");
2✔
2750
    target->add_column(type_Int, "");
2✔
2751
    auto o0 = origin->create_object();
2✔
2752
    auto t0 = target->create_object();
2✔
2753
    auto t1 = target->create_object();
2✔
2754
    auto t2 = target->create_object();
2✔
2755

1✔
2756
    auto link_list = o0.get_linklist(col0);
2✔
2757
    link_list.add(t0.get_key());
2✔
2758
    group->commit_and_continue_as_read();
2✔
2759
    CHECK_EQUAL(1, link_list.size());
2✔
2760
    group->verify();
2✔
2761
    // now change a link in link list and roll back the change
1✔
2762
    group->promote_to_write();
2✔
2763
    link_list.add(t1.get_key());
2✔
2764
    link_list.add(t2.get_key());
2✔
2765
    CHECK_EQUAL(3, link_list.size());
2✔
2766
    group->rollback_and_continue_as_read();
2✔
2767
    CHECK_EQUAL(1, link_list.size());
2✔
2768
    group->promote_to_write();
2✔
2769
    link_list.remove(0);
2✔
2770
    CHECK_EQUAL(0, link_list.size());
2✔
2771
    group->rollback_and_continue_as_read();
2✔
2772
    CHECK_EQUAL(1, link_list.size());
2✔
2773
}
2✔
2774

2775

2776
TEST(LangBindHelper_RollbackAndContinueAsRead_Links)
2777
{
2✔
2778
    SHARED_GROUP_TEST_PATH(path);
2✔
2779
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2780
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2781
    auto group = sg->start_read();
2✔
2782
    group->promote_to_write();
2✔
2783
    TableRef origin = group->add_table("origin");
2✔
2784
    TableRef target = group->add_table("target");
2✔
2785
    auto col0 = origin->add_column(*target, "");
2✔
2786
    target->add_column(type_Int, "");
2✔
2787
    auto o0 = origin->create_object();
2✔
2788
    target->create_object();
2✔
2789
    auto t1 = target->create_object();
2✔
2790
    auto t2 = target->create_object();
2✔
2791

1✔
2792
    o0.set(col0, t2.get_key());
2✔
2793
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2794
    group->commit_and_continue_as_read();
2✔
2795

1✔
2796
    // verify that we can revert a link change:
1✔
2797
    group->promote_to_write();
2✔
2798
    o0.set(col0, t1.get_key());
2✔
2799
    CHECK_EQUAL(t1.get_key(), o0.get<ObjKey>(col0));
2✔
2800
    group->rollback_and_continue_as_read();
2✔
2801
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2802
    // verify that we can revert addition of a row in target table
1✔
2803
    group->promote_to_write();
2✔
2804
    target->create_object();
2✔
2805
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2806
    group->rollback_and_continue_as_read();
2✔
2807
    CHECK_EQUAL(t2.get_key(), o0.get<ObjKey>(col0));
2✔
2808
}
2✔
2809

2810

2811
TEST(LangBindHelper_RollbackAndContinueAsRead_LinkLists)
2812
{
2✔
2813
    SHARED_GROUP_TEST_PATH(path);
2✔
2814
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2815
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2816
    auto group = sg->start_read();
2✔
2817
    group->promote_to_write();
2✔
2818
    TableRef origin = group->add_table("origin");
2✔
2819
    TableRef target = group->add_table("target");
2✔
2820
    auto col0 = origin->add_column_list(*target, "");
2✔
2821
    target->add_column(type_Int, "");
2✔
2822
    auto o0 = origin->create_object();
2✔
2823
    auto t0 = target->create_object();
2✔
2824
    auto t1 = target->create_object();
2✔
2825
    auto t2 = target->create_object();
2✔
2826

1✔
2827
    auto link_list = o0.get_linklist(col0);
2✔
2828
    link_list.add(t0.get_key());
2✔
2829
    link_list.add(t1.get_key());
2✔
2830
    link_list.add(t2.get_key());
2✔
2831
    link_list.add(t0.get_key());
2✔
2832
    link_list.add(t2.get_key());
2✔
2833
    group->commit_and_continue_as_read();
2✔
2834
    // verify that we can reverse a LinkView::move()
1✔
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
    group->promote_to_write();
2✔
2842
    link_list.move(1, 3);
2✔
2843
    CHECK_EQUAL(5, link_list.size());
2✔
2844
    CHECK_EQUAL(t0.get_key(), link_list.get(0));
2✔
2845
    CHECK_EQUAL(t2.get_key(), link_list.get(1));
2✔
2846
    CHECK_EQUAL(t0.get_key(), link_list.get(2));
2✔
2847
    CHECK_EQUAL(t1.get_key(), link_list.get(3));
2✔
2848
    CHECK_EQUAL(t2.get_key(), link_list.get(4));
2✔
2849
    group->rollback_and_continue_as_read();
2✔
2850
    CHECK_EQUAL(5, link_list.size());
2✔
2851
    CHECK_EQUAL(t0.get_key(), link_list.get(0));
2✔
2852
    CHECK_EQUAL(t1.get_key(), link_list.get(1));
2✔
2853
    CHECK_EQUAL(t2.get_key(), link_list.get(2));
2✔
2854
    CHECK_EQUAL(t0.get_key(), link_list.get(3));
2✔
2855
    CHECK_EQUAL(t2.get_key(), link_list.get(4));
2✔
2856
}
2✔
2857

2858

2859
TEST(LangBindHelper_RollbackAndContinueAsRead_TableClear)
2860
{
2✔
2861
    SHARED_GROUP_TEST_PATH(path);
2✔
2862
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2863
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2864
    auto group = sg->start_read();
2✔
2865

1✔
2866
    group->promote_to_write();
2✔
2867
    TableRef origin = group->add_table("origin");
2✔
2868
    TableRef target = group->add_table("target");
2✔
2869

1✔
2870
    auto c1 = origin->add_column_list(*target, "linklist");
2✔
2871
    target->add_column(type_Int, "int");
2✔
2872
    auto c2 = origin->add_column(*target, "link");
2✔
2873

1✔
2874
    Obj t = target->create_object();
2✔
2875
    Obj o = origin->create_object();
2✔
2876
    o.set(c2, t.get_key());
2✔
2877
    LnkLst l = o.get_linklist(c1);
2✔
2878
    l.add(t.get_key());
2✔
2879
    group->commit_and_continue_as_read();
2✔
2880

1✔
2881
    group->promote_to_write();
2✔
2882
    CHECK_EQUAL(1, l.size());
2✔
2883
    target->clear();
2✔
2884
    CHECK_EQUAL(0, l.size());
2✔
2885

1✔
2886
    group->rollback_and_continue_as_read();
2✔
2887
    CHECK_EQUAL(1, l.size());
2✔
2888
}
2✔
2889

2890
TEST(LangBindHelper_RollbackAndContinueAsRead_IntIndex)
2891
{
2✔
2892
    SHARED_GROUP_TEST_PATH(path);
2✔
2893
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2894
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2895
    auto g = sg->start_read();
2✔
2896
    g->promote_to_write();
2✔
2897

1✔
2898
    TableRef target = g->add_table("target");
2✔
2899
    ColKey col = target->add_column(type_Int, "pk");
2✔
2900
    target->add_search_index(col);
2✔
2901

1✔
2902
    std::vector<ObjKey> keys;
2✔
2903
    target->create_objects(REALM_MAX_BPNODE_SIZE + 1, keys);
2✔
2904
    g->commit_and_continue_as_read();
2✔
2905
    g->promote_to_write();
2✔
2906

1✔
2907
    // Ensure that the index has a different bptree layout so that failing to
1✔
2908
    // refresh it will do bad things
1✔
2909
    auto it = target->begin();
2✔
2910
    for (int i = 0; i < REALM_MAX_BPNODE_SIZE + 1; ++i) {
2,004✔
2911
        it->set<int64_t>(col, i);
2,002✔
2912
        ++it;
2,002✔
2913
    }
2,002✔
2914

1✔
2915
    g->rollback_and_continue_as_read();
2✔
2916
    g->promote_to_write();
2✔
2917

1✔
2918
    // Crashes if index has an invalid parent ref
1✔
2919
    target->clear();
2✔
2920
}
2✔
2921

2922

2923
TEST(LangBindHelper_ImplicitTransactions_OverSharedGroupDestruction)
2924
{
2✔
2925
    SHARED_GROUP_TEST_PATH(path);
2✔
2926
    // we hold on to write log collector and registry across a complete
1✔
2927
    // shutdown/initialization of shared rt->
1✔
2928
    std::unique_ptr<Replication> hist1(make_in_realm_history());
2✔
2929
    {
2✔
2930
        DBRef sg = DB::create(*hist1, path, DBOptions(crypt_key()));
2✔
2931
        {
2✔
2932
            WriteTransaction wt(sg);
2✔
2933
            TableRef tr = wt.add_table("table");
2✔
2934
            tr->add_column(type_Int, "first");
2✔
2935
            for (int i = 0; i < 20; i++)
42✔
2936
                tr->create_object();
40✔
2937
            wt.commit();
2✔
2938
        }
2✔
2939
        // no valid shared group anymore
1✔
2940
    }
2✔
2941
    {
2✔
2942
        std::unique_ptr<Replication> hist2(make_in_realm_history());
2✔
2943
        DBRef sg = DB::create(*hist2, path, DBOptions(crypt_key()));
2✔
2944
        {
2✔
2945
            WriteTransaction wt(sg);
2✔
2946
            TableRef tr = wt.get_table("table");
2✔
2947
            for (int i = 0; i < 20; i++)
42✔
2948
                tr->create_object();
40✔
2949
            wt.commit();
2✔
2950
        }
2✔
2951
    }
2✔
2952
}
2✔
2953

2954
TEST(LangBindHelper_ImplicitTransactions_LinkList)
2955
{
2✔
2956
    SHARED_GROUP_TEST_PATH(path);
2✔
2957
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2958
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2959
    auto group = sg->start_write();
2✔
2960
    TableRef origin = group->add_table("origin");
2✔
2961
    TableRef target = group->add_table("target");
2✔
2962
    auto col = origin->add_column_list(*target, "");
2✔
2963
    target->add_column(type_Int, "");
2✔
2964
    auto O0 = origin->create_object();
2✔
2965
    auto T0 = target->create_object();
2✔
2966
    auto link_list = O0.get_linklist(col);
2✔
2967
    link_list.add(T0.get_key());
2✔
2968
    group->commit_and_continue_as_read();
2✔
2969
    group->verify();
2✔
2970
}
2✔
2971

2972

2973
TEST(LangBindHelper_ImplicitTransactions_StringIndex)
2974
{
2✔
2975
    SHARED_GROUP_TEST_PATH(path);
2✔
2976
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
2977
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
2978
    auto group = sg->start_write();
2✔
2979
    TableRef table = group->add_table("a");
2✔
2980
    auto col = table->add_column(type_String, "b");
2✔
2981
    table->add_search_index(col);
2✔
2982
    group->verify();
2✔
2983
    group->commit_and_continue_as_read();
2✔
2984
    group->verify();
2✔
2985
}
2✔
2986

2987

2988
namespace {
2989

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

3016
void multiple_trackers_reader_thread(TestContext& test_context, DBRef db)
3017
{
6✔
3018
    // verify that consistency is maintained as we advance_read through a
3✔
3019
    // stream of transactions
3✔
3020
    auto g = db->start_read();
6✔
3021
    auto ta = g->get_table("A");
6✔
3022
    auto tb = g->get_table("B");
6✔
3023
    auto tc = g->get_table("C");
6✔
3024
    auto col = ta->get_column_keys()[0];
6✔
3025
    auto b_col = tb->get_column_keys()[0];
6✔
3026
    TableView tv = ta->where().greater(col, 100).find_all();
6✔
3027
    const auto wait_start = std::chrono::steady_clock::now();
6✔
3028
    std::chrono::seconds max_wait_seconds = std::chrono::seconds(1050);
6✔
3029
    while (tc->size() == 0) {
29,599✔
3030
        auto count = tb->begin()->get<int64_t>(b_col);
29,593✔
3031
        tv.sync_if_needed();
29,593✔
3032
        CHECK_EQUAL(tv.size(), count);
29,593✔
3033
        std::this_thread::yield();
29,593✔
3034
        g->advance_read();
29,593✔
3035
        if (std::chrono::steady_clock::now() - wait_start > max_wait_seconds) {
29,593✔
3036
            // if there is a fatal problem with a writer process we don't want the
3037
            // readers to wait forever as a spawned background processs
3038
            constexpr bool reader_process_timed_out = false;
×
3039
            REALM_ASSERT(reader_process_timed_out);
×
3040
        }
×
3041
    }
29,593✔
3042
}
6✔
3043

3044
} // anonymous namespace
3045

3046
TEST(LangBindHelper_ImplicitTransactions_MultipleTrackers)
3047
{
2✔
3048
    const int write_thread_count = 7;
2✔
3049
    const int read_thread_count = 3;
2✔
3050

1✔
3051
    SHARED_GROUP_TEST_PATH(path);
2✔
3052

1✔
3053
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3054
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3055
    {
2✔
3056
        // initialize table with 200 entries holding 0..200
1✔
3057
        WriteTransaction wt(sg);
2✔
3058
        TableRef tr = wt.add_table("A");
2✔
3059
        auto col = tr->add_column(type_Int, "first");
2✔
3060
        for (int j = 0; j < 200; j++) {
402✔
3061
            tr->create_object().set(col, j);
400✔
3062
        }
400✔
3063
        auto table_b = wt.add_table("B");
2✔
3064
        table_b->add_column(type_Int, "bussemand");
2✔
3065
        table_b->create_object().set_all(99);
2✔
3066
        wt.add_table("C");
2✔
3067
        wt.commit();
2✔
3068
    }
2✔
3069
    // FIXME: Use separate arrays for reader and writer threads for safety and readability.
1✔
3070
    Thread threads[write_thread_count + read_thread_count];
2✔
3071
    for (int i = 0; i < write_thread_count; ++i)
16✔
3072
        threads[i].start([&] {
14✔
3073
            multiple_trackers_writer_thread(sg);
14✔
3074
        });
14✔
3075
    std::this_thread::yield();
2✔
3076
    for (int i = 0; i < read_thread_count; ++i) {
8✔
3077
        threads[write_thread_count + i].start([&] {
6✔
3078
            multiple_trackers_reader_thread(test_context, sg);
6✔
3079
        });
6✔
3080
    }
6✔
3081

1✔
3082
    // Wait for all writer threads to complete
1✔
3083
    for (int i = 0; i < write_thread_count; ++i)
16✔
3084
        threads[i].join();
14✔
3085

1✔
3086
    // Allow readers time to catch up
1✔
3087
    for (int k = 0; k < 100; ++k)
202✔
3088
        std::this_thread::yield();
200✔
3089

1✔
3090
    // signal to all readers to complete
1✔
3091
    {
2✔
3092
        WriteTransaction wt(sg);
2✔
3093
        TableRef tr = wt.get_table("C");
2✔
3094
        tr->create_object();
2✔
3095
        wt.commit();
2✔
3096
    }
2✔
3097
    // Wait for all reader threads to complete
1✔
3098
    for (int i = 0; i < read_thread_count; ++i)
8✔
3099
        threads[write_thread_count + i].join();
6✔
3100
}
2✔
3101

3102
// Interprocess communication does not work with encryption enabled on Apple.
3103
// This is because fork() does not play well with Apple primitives such as
3104
// dispatch_queue_t in ReclaimerThreadStopper. This could possibly be fixed if
3105
// we need more tests like this.
3106

3107
#if !REALM_ANDROID && !REALM_IOS
3108

3109
// fork should not be used on android or ios.
3110
// This test must be non-concurrant due to fork. If a child process
3111
// is created while a static mutex is locked (eg. util::GlobalRandom::m_mutex)
3112
// then any attempt to use the mutex would hang infinitely and the child would
3113
// crash upon exit(0) when attempting to destroy a locked mutex.
3114
// This is not run with ASAN because children intentionally call exit(0) which does not
3115
// invoke destructors.
3116
NONCONCURRENT_TEST_IF(LangBindHelper_ImplicitTransactions_InterProcess, testing_supports_spawn_process)
3117
{
2✔
3118
    const int write_process_count = 7;
2✔
3119
    const int read_process_count = 3;
2✔
3120

1✔
3121
    std::vector<std::unique_ptr<SpawnedProcess>> readers;
2✔
3122
    std::vector<std::unique_ptr<SpawnedProcess>> writers;
2✔
3123
    SHARED_GROUP_TEST_PATH(path);
2✔
3124
    auto key = crypt_key();
2✔
3125
    auto process = test_util::spawn_process(test_context.test_details.test_name, "populate");
2✔
3126
    if (process->is_child()) {
2✔
3127
        try {
×
3128
            std::unique_ptr<Replication> hist(make_in_realm_history());
×
3129
            DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3130
            // initialize table with 200 entries holding 0..200
3131
            WriteTransaction wt(sg);
×
3132
            TableRef tr = wt.add_table("A");
×
3133
            auto col = tr->add_column(type_Int, "first");
×
3134
            for (int j = 0; j < 200; j++) {
×
3135
                tr->create_object().set(col, j);
×
3136
            }
×
3137
            auto table_b = wt.add_table("B");
×
3138
            table_b->add_column(type_Int, "bussemand");
×
3139
            table_b->create_object().set_all(99);
×
3140
            wt.add_table("C");
×
3141
            wt.commit();
×
3142
        }
×
3143
        catch (const std::exception& e) {
×
3144
            REALM_ASSERT_EX(false, e.what());
×
3145
            static_cast<void>(e); // e is unused without assertions on
×
3146
        }
×
3147
        exit(0);
×
3148
    }
2✔
3149

1✔
3150
    if (process->is_parent()) {
2✔
3151
        process->wait_for_child_to_finish();
2✔
3152
    }
2✔
3153

1✔
3154
    // intialization complete. Start writers:
1✔
3155
    for (int i = 0; i < write_process_count; ++i) {
16✔
3156
        writers.push_back(
14✔
3157
            test_util::spawn_process(test_context.test_details.test_name, util::format("writer[%1]", i)));
14✔
3158
        if (writers.back()->is_child()) {
14✔
3159
            {
×
3160
                // util::format(std::cout, "Writer[%1](%2) starting.\n", test_util::get_pid(), i);
3161
                std::unique_ptr<Replication> hist(make_in_realm_history());
×
3162
                DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3163
                multiple_trackers_writer_thread(sg);
×
3164
                // util::format(std::cout, "Writer[%1](%2) done.\n", test_util::get_pid(), i);
3165
            } // clean up sg before exit
×
3166
            exit(0);
×
3167
        }
×
3168
    }
14✔
3169
    std::this_thread::yield();
2✔
3170
    // then start readers:
1✔
3171
    for (int i = 0; i < read_process_count; ++i) {
8✔
3172
        readers.push_back(
6✔
3173
            test_util::spawn_process(test_context.test_details.test_name, util::format("reader[%1]", i)));
6✔
3174
        if (readers[i]->is_child()) {
6✔
3175
            {
×
3176
                // util::format(std::cout, "Reader[%1](%2) starting.\n", test_util::get_pid(), i);
3177
                std::unique_ptr<Replication> hist(make_in_realm_history());
×
3178
                DBRef sg = DB::create(*hist, path, DBOptions(key));
×
3179
                multiple_trackers_reader_thread(test_context, sg);
×
3180
                // util::format(std::cout, "Reader[%1] done.\n", i);
3181
            } // clean up sg before exit
×
3182
            exit(0);
×
3183
        }
×
3184
    }
6✔
3185

1✔
3186
    if (process->is_parent()) {
2✔
3187
        // Wait for all writer threads to complete
1✔
3188
        for (int i = 0; i < write_process_count; ++i) {
16✔
3189
            writers[i]->wait_for_child_to_finish();
14✔
3190
        }
14✔
3191

1✔
3192
        // Allow readers time to catch up
1✔
3193
        for (int k = 0; k < 100; ++k)
202✔
3194
            std::this_thread::yield();
200✔
3195

1✔
3196
        // signal to all readers to complete
1✔
3197
        {
2✔
3198
            std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3199
            DBRef sg = DB::create(*hist, path, DBOptions(key));
2✔
3200
            WriteTransaction wt(sg);
2✔
3201
            TableRef tr = wt.get_table("C");
2✔
3202
            tr->create_object();
2✔
3203
            wt.commit();
2✔
3204
        }
2✔
3205

1✔
3206
        // Wait for all reader threads to complete
1✔
3207
        for (int i = 0; i < read_process_count; ++i) {
8✔
3208
            readers[i]->wait_for_child_to_finish();
6✔
3209
        }
6✔
3210
    }
2✔
3211
}
2✔
3212

3213
#endif // !REALM_ANDROID && !REALM_IOS
3214

3215
TEST(LangBindHelper_ImplicitTransactions_NoExtremeFileSpaceLeaks)
3216
{
2✔
3217
    SHARED_GROUP_TEST_PATH(path);
2✔
3218

1✔
3219
    for (int i = 0; i < 100; ++i) {
202✔
3220
        std::unique_ptr<Replication> hist(make_in_realm_history());
200✔
3221
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
200✔
3222
        auto trans = sg->start_read();
200✔
3223
        trans->promote_to_write();
200✔
3224
        trans->commit_and_continue_as_read();
200✔
3225
    }
200✔
3226

1✔
3227
// the miminum filesize (after a commit) is one or two pages, depending on the
1✔
3228
// page size.
1✔
3229
#if REALM_ENABLE_ENCRYPTION
2✔
3230
    if (crypt_key())
2✔
3231
        // Encrypted files are always at least a 4096 byte header plus payload
1✔
3232
        CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size() + 4096);
1✔
3233
    else
2✔
3234
        CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size());
2✔
3235
#else
3236
    CHECK_LESS_EQUAL(File(path).get_size(), 2 * page_size());
3237
#endif // REALM_ENABLE_ENCRYPTION
3238
}
2✔
3239

3240

3241
TEST(LangBindHelper_ImplicitTransactions_ContinuedUseOfTable)
3242
{
2✔
3243
    SHARED_GROUP_TEST_PATH(path);
2✔
3244

1✔
3245
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3246
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3247
    auto group = sg->start_read();
2✔
3248
    auto group_w = sg->start_write();
2✔
3249

1✔
3250
    TableRef table_w = group_w->add_table("table");
2✔
3251
    auto col = table_w->add_column(type_Int, "");
2✔
3252
    auto obj = table_w->create_object();
2✔
3253
    group_w->commit_and_continue_as_read();
2✔
3254
    group_w->verify();
2✔
3255

1✔
3256
    group->advance_read();
2✔
3257
    ConstTableRef table = group->get_table("table");
2✔
3258
    CHECK_EQUAL(1, table->size());
2✔
3259
    group->verify();
2✔
3260

1✔
3261
    group_w->promote_to_write();
2✔
3262
    obj.set<int64_t>(col, 1);
2✔
3263
    group_w->commit_and_continue_as_read();
2✔
3264
    group_w->verify();
2✔
3265

1✔
3266
    group->advance_read();
2✔
3267
    auto obj2 = group->import_copy_of(obj);
2✔
3268
    CHECK_EQUAL(1, obj2.get<int64_t>(col));
2✔
3269
    group->verify();
2✔
3270
}
2✔
3271

3272

3273
TEST(LangBindHelper_ImplicitTransactions_ContinuedUseOfLinkList)
3274
{
2✔
3275
    SHARED_GROUP_TEST_PATH(path);
2✔
3276

1✔
3277
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3278
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3279
    auto group = sg->start_read();
2✔
3280
    auto group_w = sg->start_write();
2✔
3281

1✔
3282
    TableRef table_w = group_w->add_table("table");
2✔
3283
    auto col = table_w->add_column_list(*table_w, "flubber");
2✔
3284
    auto obj = table_w->create_object();
2✔
3285
    auto link_list_w = obj.get_linklist(col);
2✔
3286
    link_list_w.add(obj.get_key());
2✔
3287
    // CHECK_EQUAL(1, link_list_w.size()); // avoid this, it hides missing updates
1✔
3288
    group_w->commit_and_continue_as_read();
2✔
3289
    group_w->verify();
2✔
3290

1✔
3291
    group->advance_read();
2✔
3292
    auto link_list = obj.get_linklist(col);
2✔
3293
    CHECK_EQUAL(1, link_list.size());
2✔
3294
    group->verify();
2✔
3295

1✔
3296
    group_w->promote_to_write();
2✔
3297
    // CHECK_EQUAL(1, link_list_w.size()); // avoid this, it hides missing updates
1✔
3298
    link_list_w.add(obj.get_key());
2✔
3299
    CHECK_EQUAL(2, link_list_w.size());
2✔
3300
    group_w->commit_and_continue_as_read();
2✔
3301
    group_w->verify();
2✔
3302

1✔
3303
    group->advance_read();
2✔
3304
    CHECK_EQUAL(2, link_list.size());
2✔
3305
    group->verify();
2✔
3306
}
2✔
3307

3308

3309
TEST(LangBindHelper_MemOnly)
3310
{
2✔
3311
    SHARED_GROUP_TEST_PATH(path);
2✔
3312
    ShortCircuitHistory hist;
2✔
3313
    DBRef sg = DB::create(hist, path, DBOptions(DBOptions::Durability::MemOnly));
2✔
3314

1✔
3315
    // Verify that the db is empty after populating and then re-opening a file
1✔
3316
    {
2✔
3317
        WriteTransaction wt(sg);
2✔
3318
        wt.add_table("table");
2✔
3319
        wt.commit();
2✔
3320
    }
2✔
3321
    {
2✔
3322
        TransactionRef rt = sg->start_read();
2✔
3323
        CHECK(!rt->is_empty());
2✔
3324
    }
2✔
3325
    sg->close();
2✔
3326
    sg = DB::create(hist, path, DBOptions(DBOptions::Durability::MemOnly));
2✔
3327

1✔
3328
    // Verify that basic replication functionality works
1✔
3329
    auto rt = sg->start_read();
2✔
3330
    {
2✔
3331
        WriteTransaction wt(sg);
2✔
3332
        wt.add_table("table");
2✔
3333
        wt.commit();
2✔
3334
    }
2✔
3335

1✔
3336
    CHECK(rt->is_empty());
2✔
3337
    rt->advance_read();
2✔
3338
    CHECK(!rt->is_empty());
2✔
3339
}
2✔
3340

3341
TEST(LangBindHelper_ImplicitTransactions_SearchIndex)
3342
{
2✔
3343
    SHARED_GROUP_TEST_PATH(path);
2✔
3344

1✔
3345
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3346
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3347
    auto rt = sg->start_read();
2✔
3348
    auto group_w = sg->start_read();
2✔
3349

1✔
3350
    // Add initial data
1✔
3351
    group_w->promote_to_write();
2✔
3352
    TableRef table_w = group_w->add_table("table");
2✔
3353
    auto c0 = table_w->add_column(type_Int, "int1");
2✔
3354
    auto c1 = table_w->add_column(type_String, "str");
2✔
3355
    auto c2 = table_w->add_column(type_Int, "int2");
2✔
3356
    auto ok = table_w->create_object(ObjKey{}, {{c1, "2"}, {c0, 1}, {c2, 3}}).get_key();
2✔
3357
    group_w->commit_and_continue_as_read();
2✔
3358
    group_w->verify();
2✔
3359

1✔
3360
    rt->advance_read();
2✔
3361
    ConstTableRef table = rt->get_table("table");
2✔
3362
    auto obj = table->get_object(ok);
2✔
3363
    CHECK_EQUAL(1, obj.get<int64_t>(c0));
2✔
3364
    CHECK_EQUAL("2", obj.get<StringData>(c1));
2✔
3365
    CHECK_EQUAL(3, obj.get<int64_t>(c2));
2✔
3366
    rt->verify();
2✔
3367

1✔
3368
    // Add search index and re-verify
1✔
3369
    group_w->promote_to_write();
2✔
3370
    table_w->add_search_index(c1);
2✔
3371
    group_w->commit_and_continue_as_read();
2✔
3372
    group_w->verify();
2✔
3373

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

1✔
3381
    // Remove search index and re-verify
1✔
3382
    group_w->promote_to_write();
2✔
3383
    table_w->remove_search_index(c1);
2✔
3384
    group_w->commit_and_continue_as_read();
2✔
3385
    group_w->verify();
2✔
3386

1✔
3387
    rt->advance_read();
2✔
3388
    CHECK_EQUAL(1, obj.get<int64_t>(c0));
2✔
3389
    CHECK_EQUAL("2", obj.get<StringData>(c1));
2✔
3390
    CHECK_EQUAL(3, obj.get<int64_t>(c2));
2✔
3391
    CHECK(!table->has_search_index(c1));
2✔
3392
    rt->verify();
2✔
3393
}
2✔
3394

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

3437
TEST(LangBindHelper_SubqueryHandoverQueryCreatedFromDeletedLinkView)
3438
{
2✔
3439
    SHARED_GROUP_TEST_PATH(path);
2✔
3440
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3441
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3442
    TransactionRef reader;
2✔
3443
    auto writer = sg->start_write();
2✔
3444
    {
2✔
3445
        TableView tv1;
2✔
3446
        auto table = writer->add_table("table");
2✔
3447
        auto table2 = writer->add_table("table2");
2✔
3448
        table2->add_column(type_Int, "int");
2✔
3449
        auto key = table2->create_object().set_all(42).get_key();
2✔
3450

1✔
3451
        auto col = table->add_column_list(*table2, "first");
2✔
3452
        auto obj = table->create_object();
2✔
3453
        auto link_view = obj.get_linklist(col);
2✔
3454

1✔
3455
        link_view.add(key);
2✔
3456
        writer->commit_and_continue_as_read();
2✔
3457

1✔
3458
        Query qq = table2->where(link_view);
2✔
3459
        CHECK_EQUAL(qq.count(), 1);
2✔
3460
        writer->promote_to_write();
2✔
3461
        table->clear();
2✔
3462
        writer->commit_and_continue_as_read();
2✔
3463
        CHECK_EQUAL(link_view.size(), 0);
2✔
3464
        CHECK_EQUAL(qq.count(), 0);
2✔
3465

1✔
3466
        reader = writer->duplicate();
2✔
3467
#ifdef OLD_CORE_BEHAVIOR
3468
        // FIXME: Old core would allow the code below, but new core will throw.
3469
        //
3470
        // Why should a query still be valid after a change, when it would not be possible
3471
        // to reconstruct the query from new after said change?
3472
        //
3473
        // In this specific case, the query is constructed from a linkview on an object
3474
        // which is destroyed. After the object is destroyed, the linkview obviously
3475
        // cannot be constructed, and hence the query can also not be constructed.
3476
        auto lv2 = reader->import_copy_of(link_view);
3477
        auto rq = reader->import_copy_of(qq, PayloadPolicy::Copy);
3478
        writer->close();
3479
        auto tv = rq->find_all();
3480

3481
        CHECK(tv.is_in_sync());
3482
        CHECK(tv.is_attached());
3483
        CHECK_EQUAL(0, tv.size());
3484
#endif
3485
    }
2✔
3486
}
2✔
3487

3488

3489
TEST(LangBindHelper_SubqueryHandoverDependentViews)
3490
{
2✔
3491
    SHARED_GROUP_TEST_PATH(path);
2✔
3492
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3493
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3494
    std::unique_ptr<Query> qq2;
2✔
3495
    TransactionRef reader;
2✔
3496
    ColKey col1;
2✔
3497
    {
2✔
3498
        {
2✔
3499
            TableView tv1;
2✔
3500
            auto writer = sg->start_write();
2✔
3501
            TableRef table = writer->add_table("table2");
2✔
3502
            auto col0 = table->add_column(type_Int, "first");
2✔
3503
            col1 = table->add_column(type_Bool, "even");
2✔
3504
            for (int i = 0; i < 100; ++i) {
202✔
3505
                auto obj = table->create_object();
200✔
3506
                obj.set<int>(col0, i);
200✔
3507
                bool isEven = ((i % 2) == 0);
200✔
3508
                obj.set<bool>(col1, isEven);
200✔
3509
            }
200✔
3510
            writer->commit_and_continue_as_read();
2✔
3511
            tv1 = table->where().less_equal(col0, 50).find_all();
2✔
3512
            Query qq = tv1.get_parent()->where(&tv1);
2✔
3513
            reader = writer->duplicate();
2✔
3514
            qq2 = reader->import_copy_of(qq, PayloadPolicy::Copy);
2✔
3515
            CHECK(tv1.is_attached());
2✔
3516
            CHECK_EQUAL(51, tv1.size());
2✔
3517
        }
2✔
3518
        {
2✔
3519
            realm::TableView tv = qq2->equal(col1, true).find_all();
2✔
3520

1✔
3521
            CHECK(tv.is_in_sync());
2✔
3522
            CHECK(tv.is_attached());
2✔
3523
            CHECK_EQUAL(26, tv.size()); // BOOM! fail with 50
2✔
3524
        }
2✔
3525
    }
2✔
3526
}
2✔
3527

3528
TEST(LangBindHelper_HandoverPartialQuery)
3529
{
2✔
3530
    SHARED_GROUP_TEST_PATH(path);
2✔
3531
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3532
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3533
    std::unique_ptr<Query> qq2;
2✔
3534
    TransactionRef reader;
2✔
3535
    ColKey col0;
2✔
3536
    {
2✔
3537
        {
2✔
3538
            TableView tv1;
2✔
3539
            auto writer = sg->start_write();
2✔
3540
            TableRef table = writer->add_table("table2");
2✔
3541
            col0 = table->add_column(type_Int, "first");
2✔
3542
            auto col1 = table->add_column(type_Bool, "even");
2✔
3543
            for (int i = 0; i < 100; ++i) {
202✔
3544
                auto obj = table->create_object();
200✔
3545
                obj.set<int>(col0, i);
200✔
3546
                bool isEven = ((i % 2) == 0);
200✔
3547
                obj.set<bool>(col1, isEven);
200✔
3548
            }
200✔
3549
            writer->commit_and_continue_as_read();
2✔
3550
            tv1 = table->where().less_equal(col0, 50).find_all();
2✔
3551
            Query qq = tv1.get_parent()->where(&tv1);
2✔
3552
            reader = writer->duplicate();
2✔
3553
            qq2 = reader->import_copy_of(qq, PayloadPolicy::Copy);
2✔
3554
            CHECK(tv1.is_attached());
2✔
3555
            CHECK_EQUAL(51, tv1.size());
2✔
3556
        }
2✔
3557
        {
2✔
3558
            TableView tv = qq2->greater(col0, 48).find_all();
2✔
3559
            CHECK(tv.is_attached());
2✔
3560
            CHECK_EQUAL(2, tv.size());
2✔
3561
            auto obj = tv.get_object(0);
2✔
3562
            CHECK_EQUAL(49, obj.get<int64_t>(col0));
2✔
3563
            obj = tv.get_object(1);
2✔
3564
            CHECK_EQUAL(50, obj.get<int64_t>(col0));
2✔
3565
        }
2✔
3566
    }
2✔
3567
}
2✔
3568

3569

3570
// Verify that an in-sync TableView backed by a Query that is restricted to a TableView
3571
// remains in sync when handed-over using a mutable payload.
3572
TEST(LangBindHelper_HandoverNestedTableViews)
3573
{
2✔
3574
    SHARED_GROUP_TEST_PATH(path);
2✔
3575
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3576
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3577
    {
2✔
3578
        TransactionRef reader;
2✔
3579
        std::unique_ptr<TableView> tv;
2✔
3580
        {
2✔
3581
            auto writer = sg->start_write();
2✔
3582
            TableRef table = writer->add_table("table2");
2✔
3583
            auto col = table->add_column(type_Int, "first");
2✔
3584
            for (int i = 0; i < 100; ++i) {
202✔
3585
                table->create_object().set_all(i);
200✔
3586
            }
200✔
3587
            writer->commit_and_continue_as_read();
2✔
3588
            // Create a TableView tv2 that is backed by a Query that is restricted to rows from TableView tv1.
1✔
3589
            TableView tv1 = table->where().less_equal(col, 50).find_all();
2✔
3590
            TableView tv2 = tv1.get_parent()->where(&tv1).greater(col, 25).find_all();
2✔
3591
            CHECK(tv2.is_in_sync());
2✔
3592
            reader = writer->duplicate();
2✔
3593
            tv = reader->import_copy_of(tv2, PayloadPolicy::Move);
2✔
3594
        }
2✔
3595
        CHECK(tv->is_in_sync());
2✔
3596
        CHECK(tv->is_attached());
2✔
3597
        CHECK_EQUAL(25, tv->size());
2✔
3598
    }
2✔
3599
}
2✔
3600

3601

3602
TEST(LangBindHelper_HandoverAccessors)
3603
{
2✔
3604
    SHARED_GROUP_TEST_PATH(path);
2✔
3605
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3606
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3607
    TransactionRef reader;
2✔
3608
    ColKey col;
2✔
3609
    std::unique_ptr<TableView> tv2;
2✔
3610
    std::unique_ptr<TableView> tv3;
2✔
3611
    std::unique_ptr<TableView> tv4;
2✔
3612
    std::unique_ptr<TableView> tv5;
2✔
3613
    std::unique_ptr<TableView> tv6;
2✔
3614
    std::unique_ptr<TableView> tv7;
2✔
3615
    {
2✔
3616
        TableView tv;
2✔
3617
        auto writer = sg->start_write();
2✔
3618
        TableRef table = writer->add_table("table2");
2✔
3619
        col = table->add_column(type_Int, "first");
2✔
3620
        for (int i = 0; i < 100; ++i) {
202✔
3621
            table->create_object().set_all(i);
200✔
3622
        }
200✔
3623
        writer->commit_and_continue_as_read();
2✔
3624

1✔
3625
        tv = table->where().find_all();
2✔
3626
        CHECK(tv.is_attached());
2✔
3627
        CHECK_EQUAL(100, tv.size());
2✔
3628
        for (int i = 0; i < 100; ++i)
202✔
3629
            CHECK_EQUAL(i, tv.get_object(i).get<Int>(col));
200✔
3630

1✔
3631
        reader = writer->duplicate();
2✔
3632
        tv2 = reader->import_copy_of(tv, PayloadPolicy::Copy);
2✔
3633
        CHECK(tv.is_attached());
2✔
3634
        CHECK(tv.is_in_sync());
2✔
3635

1✔
3636
        tv3 = reader->import_copy_of(tv, PayloadPolicy::Stay);
2✔
3637
        CHECK(tv.is_attached());
2✔
3638
        CHECK(tv.is_in_sync());
2✔
3639

1✔
3640
        tv4 = reader->import_copy_of(tv, PayloadPolicy::Move);
2✔
3641
        CHECK(tv.is_attached());
2✔
3642
        CHECK(!tv.is_in_sync());
2✔
3643

1✔
3644
        // and again, but this time with the source out of sync:
1✔
3645
        tv5 = reader->import_copy_of(tv, PayloadPolicy::Copy);
2✔
3646
        CHECK(tv.is_attached());
2✔
3647
        CHECK(!tv.is_in_sync());
2✔
3648

1✔
3649
        tv6 = reader->import_copy_of(tv, PayloadPolicy::Stay);
2✔
3650
        CHECK(tv.is_attached());
2✔
3651
        CHECK(!tv.is_in_sync());
2✔
3652

1✔
3653
        tv7 = reader->import_copy_of(tv, PayloadPolicy::Move);
2✔
3654
        CHECK(tv.is_attached());
2✔
3655
        CHECK(!tv.is_in_sync());
2✔
3656

1✔
3657
        // and verify, that even though it was out of sync, we can bring it in sync again
1✔
3658
        tv.sync_if_needed();
2✔
3659
        CHECK(tv.is_in_sync());
2✔
3660

1✔
3661
        // Obj handover tested elsewhere
1✔
3662
    }
2✔
3663
    {
2✔
3664
        // now examining stuff handed over to other transaction
1✔
3665
        // with payload:
1✔
3666
        CHECK(tv2->is_attached());
2✔
3667
        CHECK(tv2->is_in_sync());
2✔
3668
        CHECK_EQUAL(100, tv2->size());
2✔
3669
        for (int i = 0; i < 100; ++i)
202✔
3670
            CHECK_EQUAL(i, tv2->get_object(i).get<Int>(col));
200✔
3671
        // importing one without payload:
1✔
3672
        CHECK(tv3->is_attached());
2✔
3673
        CHECK(!tv3->is_in_sync());
2✔
3674
        tv3->sync_if_needed();
2✔
3675
        CHECK_EQUAL(100, tv3->size());
2✔
3676
        for (int i = 0; i < 100; ++i)
202✔
3677
            CHECK_EQUAL(i, tv3->get_object(i).get<Int>(col));
200✔
3678

1✔
3679
        // one with payload:
1✔
3680
        CHECK(tv4->is_attached());
2✔
3681
        CHECK(tv4->is_in_sync());
2✔
3682
        CHECK_EQUAL(100, tv4->size());
2✔
3683
        for (int i = 0; i < 100; ++i)
202✔
3684
            CHECK_EQUAL(i, tv4->get_object(i).get<Int>(col));
200✔
3685

1✔
3686
        // verify that subsequent imports are all without payload:
1✔
3687
        CHECK(tv5->is_attached());
2✔
3688
        CHECK(!tv5->is_in_sync());
2✔
3689

1✔
3690
        CHECK(tv6->is_attached());
2✔
3691
        CHECK(!tv6->is_in_sync());
2✔
3692

1✔
3693
        CHECK(tv7->is_attached());
2✔
3694
        CHECK(!tv7->is_in_sync());
2✔
3695
    }
2✔
3696
}
2✔
3697

3698
TEST(LangBindHelper_TableViewAndTransactionBoundaries)
3699
{
2✔
3700
    SHARED_GROUP_TEST_PATH(path);
2✔
3701
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3702
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3703
    ColKey col;
2✔
3704
    {
2✔
3705
        WriteTransaction wt(sg);
2✔
3706
        auto table = wt.add_table("myTable");
2✔
3707
        col = table->add_column(type_Int, "myColumn");
2✔
3708
        table->create_object().set_all(42);
2✔
3709
        wt.commit();
2✔
3710
    }
2✔
3711
    auto rt = sg->start_read();
2✔
3712
    auto tv = rt->get_table("myTable")->where().greater(col, 40).find_all();
2✔
3713
    CHECK(tv.is_in_sync());
2✔
3714
    {
2✔
3715
        WriteTransaction wt(sg);
2✔
3716
        wt.commit();
2✔
3717
    }
2✔
3718
    rt->advance_read();
2✔
3719
    CHECK(tv.is_in_sync());
2✔
3720
    {
2✔
3721
        WriteTransaction wt(sg);
2✔
3722
        wt.commit();
2✔
3723
    }
2✔
3724
    rt->promote_to_write();
2✔
3725
    CHECK(tv.is_in_sync());
2✔
3726
    rt->commit_and_continue_as_read();
2✔
3727
    CHECK(tv.is_in_sync());
2✔
3728
    {
2✔
3729
        WriteTransaction wt(sg);
2✔
3730
        auto table = wt.get_table("myTable");
2✔
3731
        table->begin()->set_all(41);
2✔
3732
        wt.commit();
2✔
3733
    }
2✔
3734
    rt->advance_read();
2✔
3735
    CHECK(!tv.is_in_sync());
2✔
3736
    tv.sync_if_needed();
2✔
3737
    CHECK(tv.is_in_sync());
2✔
3738
    rt->advance_read();
2✔
3739
    CHECK(tv.is_in_sync());
2✔
3740
}
2✔
3741

3742
namespace {
3743
// support threads for handover test. The setup is as follows:
3744
// thread A writes a stream of updates to the database,
3745
// thread B listens and continously does advance_read to see the updates.
3746
// thread B also has a table view, which it continuosly keeps in sync in response
3747
// to the updates. It then hands over the result to thread C.
3748
// thread C continuously recieves copies of the results obtained in thead B and
3749
// verifies them (by comparing with its own local, but identical query)
3750

3751
template <typename T>
3752
struct HandoverControl {
3753
    Mutex m_lock;
3754
    CondVar m_changed;
3755
    std::unique_ptr<T> m_handover;
3756
    bool m_has_feedback = false;
3757
    void put(std::unique_ptr<T> h)
3758
    {
2,004✔
3759
        LockGuard lg(m_lock);
2,004✔
3760
        // std::cout << "put " << h << std::endl;
1,027✔
3761
        while (m_handover != nullptr)
2,004✔
3762
            m_changed.wait(lg);
×
3763
        // std::cout << " -- put " << h << std::endl;
1,027✔
3764
        m_handover = std::move(h);
2,004✔
3765
        m_changed.notify_all();
2,004✔
3766
    }
2,004✔
3767
    void get(std::unique_ptr<T>& h)
3768
    {
2,004✔
3769
        LockGuard lg(m_lock);
2,004✔
3770
        // std::cout << "get " << std::endl;
1,027✔
3771
        while (m_handover == nullptr)
2,388✔
3772
            m_changed.wait(lg);
384✔
3773
        // std::cout << " -- get " << m_handover << std::endl;
1,027✔
3774
        h = std::move(m_handover);
2,004✔
3775
        m_handover = nullptr;
2,004✔
3776
        m_changed.notify_all();
2,004✔
3777
    }
2,004✔
3778
    bool try_get(std::unique_ptr<T>& h)
3779
    {
3780
        LockGuard lg(m_lock);
3781
        if (m_handover == nullptr)
3782
            return false;
3783
        h = std::move(m_handover);
3784
        m_handover = nullptr;
3785
        m_changed.notify_all();
3786
        return true;
3787
    }
3788
    void signal_feedback()
3789
    {
2,004✔
3790
        LockGuard lg(m_lock);
2,004✔
3791
        m_has_feedback = true;
2,004✔
3792
        m_changed.notify_all();
2,004✔
3793
    }
2,004✔
3794
    void wait_feedback()
3795
    {
2,004✔
3796
        LockGuard lg(m_lock);
2,004✔
3797
        while (!m_has_feedback)
4,312✔
3798
            m_changed.wait(lg);
2,308✔
3799
        m_has_feedback = false;
2,004✔
3800
    }
2,004✔
3801
    HandoverControl(const HandoverControl&) = delete;
3802
    HandoverControl() {}
2✔
3803
};
3804

3805
void handover_writer(DBRef db)
3806
{
2✔
3807
    //    std::unique_ptr<Replication> hist(make_in_realm_history());
1✔
3808
    //    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
1✔
3809
    auto g = db->start_read();
2✔
3810
    auto table = g->get_table("table");
2✔
3811
    Random random(random_int<unsigned long>());
2✔
3812
    for (int i = 1; i < 5000; ++i) {
10,000✔
3813
        g->promote_to_write();
9,998✔
3814
        // table holds random numbers >= 1, until the writing process
4,999✔
3815
        // finishes, after n new entry with value 0 is added to signal termination
4,999✔
3816
        table->create_object().set_all(1 + random.draw_int_mod(100));
9,998✔
3817
        g->commit_and_continue_as_read();
9,998✔
3818
        // improve chance of consumers running concurrently with
4,999✔
3819
        // new writes:
4,999✔
3820
        for (int n = 0; n < 10; ++n)
109,978✔
3821
            std::this_thread::yield();
99,980✔
3822
    }
9,998✔
3823
    g->promote_to_write();
2✔
3824
    table->create_object().set_all(0); // <---- signals other threads to stop
2✔
3825
    g->commit();
2✔
3826
}
2✔
3827

3828
struct Work {
3829
    TransactionRef tr;
3830
    std::unique_ptr<TableView> tv;
3831
};
3832

3833
void handover_querier(HandoverControl<Work>* control, TestContext& test_context, DBRef db)
3834
{
2✔
3835
    // We need to ensure that the initial version observed is *before* the final
1✔
3836
    // one written by the writer thread. We do this (simplisticly) by locking on
1✔
3837
    // to the initial version before even starting the writer.
1✔
3838
    auto g = db->start_read();
2✔
3839
    Thread writer;
2✔
3840
    writer.start([&] {
2✔
3841
        handover_writer(db);
2✔
3842
    });
2✔
3843
    TableRef table = g->get_table("table");
2✔
3844
    ColKeys cols = table->get_column_keys();
2✔
3845
    TableView tv = table->where().greater(cols[0], 50).find_all();
2✔
3846
    for (;;) {
1,957,030✔
3847
        // wait here for writer to change the database. Kind of wasteful, but wait_for_change()
1,269,994✔
3848
        // is not available on osx.
1,269,994✔
3849
        if (!db->has_changed(g)) {
1,957,030✔
3850
            std::this_thread::yield();
1,955,026✔
3851
            continue;
1,955,026✔
3852
        }
1,955,026✔
3853

1,027✔
3854
        g->advance_read();
2,004✔
3855
        CHECK(!tv.is_in_sync());
2,004✔
3856
        tv.sync_if_needed();
2,004✔
3857
        CHECK(tv.is_in_sync());
2,004✔
3858
        auto ref = g->duplicate();
2,004✔
3859
        std::unique_ptr<Work> h = std::make_unique<Work>();
2,004✔
3860
        h->tr = ref;
2,004✔
3861
        h->tv = ref->import_copy_of(tv, PayloadPolicy::Move);
2,004✔
3862
        control->put(std::move(h));
2,004✔
3863

1,027✔
3864
        // here we need to allow the reciever to get hold on the proper version before
1,027✔
3865
        // we go through the loop again and advance_read().
1,027✔
3866
        control->wait_feedback();
2,004✔
3867
        std::this_thread::yield();
2,004✔
3868

1,027✔
3869
        if (table->where().equal(cols[0], 0).count() >= 1)
2,004✔
3870
            break;
2✔
3871
    }
2,004✔
3872
    g->end_read();
2✔
3873
    writer.join();
2✔
3874
}
2✔
3875

3876
void handover_verifier(HandoverControl<Work>* control, TestContext& test_context)
3877
{
2✔
3878
    bool not_done = true;
2✔
3879
    while (not_done) {
2,006✔
3880
        std::unique_ptr<Work> work;
2,004✔
3881
        control->get(work);
2,004✔
3882

1,027✔
3883
        auto g = work->tr;
2,004✔
3884
        control->signal_feedback();
2,004✔
3885
        TableRef table = g->get_table("table");
2,004✔
3886
        ColKeys cols = table->get_column_keys();
2,004✔
3887
        TableView tv = table->where().greater(cols[0], 50).find_all();
2,004✔
3888
        CHECK(tv.is_in_sync());
2,004✔
3889
        std::unique_ptr<TableView> tv2 = std::move(work->tv);
2,004✔
3890
        CHECK(tv.is_in_sync());
2,004✔
3891
        CHECK(tv2->is_in_sync());
2,004✔
3892
        CHECK_EQUAL(tv.size(), tv2->size());
2,004✔
3893
        for (size_t k = 0; k < tv.size(); ++k) {
1,513,474✔
3894
            auto o = tv.get_object(k);
1,511,470✔
3895
            auto o2 = tv2->get_object(k);
1,511,470✔
3896
            CHECK_EQUAL(o.get<int64_t>(cols[0]), o2.get<int64_t>(cols[0]));
1,511,470✔
3897
        }
1,511,470✔
3898
        if (table->where().equal(cols[0], 0).count() >= 1)
2,004✔
3899
            not_done = false;
2✔
3900
        g->close();
2,004✔
3901
    }
2,004✔
3902
}
2✔
3903

3904
} // anonymous namespace
3905

3906
namespace {
3907

3908
void attacher(std::string path, ColKey col)
3909
{
20✔
3910
    // Creating a new DB in each attacher is on purpose, since we're
10✔
3911
    // testing races in the attachment process, and that only takes place
10✔
3912
    // during creation of the DB object.
10✔
3913
    std::unique_ptr<Replication> hist(make_in_realm_history());
20✔
3914
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
20✔
3915
    for (int i = 0; i < 100; ++i) {
2,020✔
3916
        auto g = sg->start_read();
2,000✔
3917
        g->verify();
2,000✔
3918
        auto table = g->get_table("table");
2,000✔
3919
        g->promote_to_write();
2,000✔
3920
        auto o = table->get_object(ObjKey(i));
2,000✔
3921
        auto o2 = table->get_object(ObjKey(i * 10));
2,000✔
3922
        o.set<int64_t>(col, 1 + o2.get<int64_t>(col));
2,000✔
3923
        g->commit_and_continue_as_read();
2,000✔
3924
        g->verify();
2,000✔
3925
        g->end_read();
2,000✔
3926
    }
2,000✔
3927
}
20✔
3928
} // anonymous namespace
3929

3930

3931
// Disable with TSAN because it needs to synchronize between multiple DBs, and TSAN isn't able to track
3932
// acquire/release across multiple mappings of the same underlying memory.
3933
TEST_IF(LangBindHelper_RacingAttachers, !running_with_tsan)
3934
{
2✔
3935
    const int num_attachers = 10;
2✔
3936
    SHARED_GROUP_TEST_PATH(path);
2✔
3937
    ColKey col;
2✔
3938
    {
2✔
3939
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3940
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3941
        auto g = sg->start_write();
2✔
3942
        auto table = g->add_table("table");
2✔
3943
        col = table->add_column(type_Int, "first");
2✔
3944
        for (int i = 0; i < 1000; ++i)
2,002✔
3945
            table->create_object(ObjKey(i));
2,000✔
3946
        g->commit();
2✔
3947
    }
2✔
3948
    Thread attachers[num_attachers];
2✔
3949
    for (int i = 0; i < num_attachers; ++i) {
22✔
3950
        attachers[i].start([&] {
20✔
3951
            attacher(path, col);
20✔
3952
        });
20✔
3953
    }
20✔
3954
    for (int i = 0; i < num_attachers; ++i) {
22✔
3955
        attachers[i].join();
20✔
3956
    }
20✔
3957
}
2✔
3958

3959
// This test takes a very long time when running with valgrind
3960
TEST_IF(LangBindHelper_HandoverBetweenThreads, !running_with_valgrind)
3961
{
2✔
3962
    SHARED_GROUP_TEST_PATH(path);
2✔
3963
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3964
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3965
    auto g = sg->start_write();
2✔
3966
    auto table = g->add_table("table");
2✔
3967
    table->add_column(type_Int, "first");
2✔
3968
    g->commit();
2✔
3969
    g = sg->start_read();
2✔
3970
    table = g->get_table("table");
2✔
3971
    CHECK(bool(table));
2✔
3972
    g->end_read();
2✔
3973

1✔
3974
    HandoverControl<Work> control;
2✔
3975
    Thread querier, verifier;
2✔
3976
    querier.start([&] {
2✔
3977
        handover_querier(&control, test_context, sg);
2✔
3978
    });
2✔
3979
    verifier.start([&] {
2✔
3980
        handover_verifier(&control, test_context);
2✔
3981
    });
2✔
3982
    querier.join();
2✔
3983
    verifier.join();
2✔
3984
}
2✔
3985

3986

3987
TEST(LangBindHelper_HandoverDependentViews)
3988
{
2✔
3989
    SHARED_GROUP_TEST_PATH(path);
2✔
3990
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
3991
    DBRef db = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
3992
    TransactionRef tr;
2✔
3993
    std::unique_ptr<TableView> tv_ov;
2✔
3994
    ColKey col;
2✔
3995
    {
2✔
3996
        // Untyped interface
1✔
3997
        {
2✔
3998
            TableView tv1;
2✔
3999
            TableView tv2;
2✔
4000
            auto group_w = db->start_write();
2✔
4001
            TableRef table = group_w->add_table("table2");
2✔
4002
            col = table->add_column(type_Int, "first");
2✔
4003
            for (int i = 0; i < 100; ++i) {
202✔
4004
                table->create_object().set_all(i);
200✔
4005
            }
200✔
4006
            group_w->commit_and_continue_as_read();
2✔
4007
            tv1 = table->where().find_all();
2✔
4008
            tv2 = table->where(&tv1).find_all();
2✔
4009
            CHECK(tv1.is_attached());
2✔
4010
            CHECK(tv2.is_attached());
2✔
4011
            CHECK_EQUAL(100, tv1.size());
2✔
4012
            for (int i = 0; i < 100; ++i) {
202✔
4013
                auto o = tv1.get_object(i);
200✔
4014
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4015
            }
200✔
4016
            CHECK_EQUAL(100, tv2.size());
2✔
4017
            for (int i = 0; i < 100; ++i) {
202✔
4018
                auto o = tv2.get_object(i);
200✔
4019
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4020
            }
200✔
4021
            tr = group_w->duplicate();
2✔
4022
            tv_ov = tr->import_copy_of(tv2, PayloadPolicy::Copy);
2✔
4023
            CHECK(tv1.is_attached());
2✔
4024
            CHECK(tv2.is_attached());
2✔
4025
        }
2✔
4026
        {
2✔
4027
            CHECK(tv_ov->is_in_sync());
2✔
4028
            // CHECK(tv1.is_attached());
1✔
4029
            CHECK(tv_ov->is_attached());
2✔
4030
            CHECK_EQUAL(100, tv_ov->size());
2✔
4031
            for (int i = 0; i < 100; ++i) {
202✔
4032
                auto o = tv_ov->get_object(i);
200✔
4033
                CHECK_EQUAL(i, o.get<int64_t>(col));
200✔
4034
            }
200✔
4035
        }
2✔
4036
    }
2✔
4037
}
2✔
4038

4039

4040
TEST(LangBindHelper_HandoverTableViewWithLnkLst)
4041
{
2✔
4042
    // First iteration hands-over a normal valid attached LnkLst. Second
1✔
4043
    // iteration hands-over a detached LnkLst.
1✔
4044
    for (int detached = 0; detached < 2; detached++) {
6✔
4045
        SHARED_GROUP_TEST_PATH(path);
4✔
4046
        std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
4047
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
4048
        ColKey col_link2, col0;
4✔
4049
        ObjKey ok0, ok1, ok2;
4✔
4050
        TransactionRef tr;
4✔
4051
        std::unique_ptr<TableView> tv2;
4✔
4052
        std::unique_ptr<Query> q2;
4✔
4053
        {
4✔
4054
            TableView tv;
4✔
4055
            auto group_w = sg->start_write();
4✔
4056

2✔
4057
            TableRef table1 = group_w->add_table("table1");
4✔
4058
            TableRef table2 = group_w->add_table("table2");
4✔
4059

2✔
4060
            // add some more columns to table1 and table2
2✔
4061
            col0 = table1->add_column(type_Int, "col1");
4✔
4062
            table1->add_column(type_String, "str1");
4✔
4063

2✔
4064
            // add some rows
2✔
4065
            ok0 = table1->create_object().set_all(300, "delta").get_key();
4✔
4066
            ok1 = table1->create_object().set_all(100, "alfa").get_key();
4✔
4067
            ok2 = table1->create_object().set_all(200, "beta").get_key();
4✔
4068

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

2✔
4071
            auto o = table2->create_object();
4✔
4072
            auto lvr = o.get_linklist(col_link2);
4✔
4073
            lvr.clear();
4✔
4074
            lvr.add(ok0);
4✔
4075
            lvr.add(ok1);
4✔
4076
            lvr.add(ok2);
4✔
4077

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

2✔
4080
            // q.m_table = table1
2✔
4081
            // q.m_view = lvr
2✔
4082
            Query q = table1->where(lvr).and_query(table1->column<Int>(col0) > 100);
4✔
4083

2✔
4084
            // Remove the LinkList that the query depends on, to see if a detached
2✔
4085
            // LinkList can be handed over correctly
2✔
4086
            if (detached == 1)
4✔
4087
                table2->remove_object(o.get_key());
2✔
4088

2✔
4089
            tv = q.find_all(); // tv = { 0, 2 } (only first iteration)
4✔
4090
            CHECK(tv.is_in_sync());
4✔
4091
            group_w->commit_and_continue_as_read();
4✔
4092
            tr = group_w->duplicate();
4✔
4093
            CHECK(tv.is_in_sync());
4✔
4094
            tv2 = tr->import_copy_of(tv, PayloadPolicy::Copy);
4✔
4095
            q2 = tr->import_copy_of(q, PayloadPolicy::Copy);
4✔
4096
            auto tv3a = q.find_all();
4✔
4097
            auto tv3b = q2->find_all();
4✔
4098
        }
4✔
4099
        {
4✔
4100
            auto tv3 = q2->find_all();
4✔
4101
            CHECK(tv2->is_in_sync());
4✔
4102
            if (detached == 0) {
4✔
4103
                CHECK_EQUAL(2, tv2->size());
2✔
4104
                CHECK_EQUAL(ok0, tv2->get_key(0));
2✔
4105
                CHECK_EQUAL(ok2, tv2->get_key(1));
2✔
4106
                CHECK_EQUAL(2, tv3.size());
2✔
4107
                CHECK_EQUAL(ok0, tv3.get_key(0));
2✔
4108
                CHECK_EQUAL(ok2, tv3.get_key(1));
2✔
4109
            }
2✔
4110
            else {
2✔
4111
                CHECK_EQUAL(0, tv2->size());
2✔
4112
                CHECK_EQUAL(0, tv3.size());
2✔
4113
            }
2✔
4114
            tr->close();
4✔
4115
        }
4✔
4116
    }
4✔
4117
}
2✔
4118

4119

4120
TEST(LangBindHelper_HandoverTableViewWithQueryOnLink)
4121
{
2✔
4122
    for (int detached = 0; detached < 2; detached++) {
6✔
4123
        SHARED_GROUP_TEST_PATH(path);
4✔
4124
        std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
4125
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
4126
        TransactionRef tr;
4✔
4127
        ObjKey target;
4✔
4128
        std::unique_ptr<TableView> tv2;
4✔
4129
        std::unique_ptr<Query> q2;
4✔
4130
        {
4✔
4131
            auto group_w = sg->start_write();
4✔
4132

2✔
4133
            TableRef table1 = group_w->add_table("table1");
4✔
4134
            TableRef table2 = group_w->add_table("table2");
4✔
4135
            table1->add_column(type_Int, "col1");
4✔
4136
            auto col_link = table2->add_column(*table1, "link");
4✔
4137

2✔
4138
            target = table1->create_object().set_all(300).get_key();
4✔
4139
            auto o = table2->create_object().set_all(target);
4✔
4140
            Query q = table2->where().and_query(table2->column<Link>(col_link) == table1->get_object(target));
4✔
4141

2✔
4142
            // Remove the object that the query depends on, to see if a detached
2✔
4143
            // object can be handed over correctly
2✔
4144
            if (detached == 1)
4✔
4145
                table2->remove_object(o.get_key());
2✔
4146

2✔
4147
            auto tv = q.find_all();
4✔
4148
            CHECK(tv.is_in_sync());
4✔
4149
            group_w->commit_and_continue_as_read();
4✔
4150
            tr = group_w->duplicate();
4✔
4151
            CHECK(tv.is_in_sync());
4✔
4152
            tv2 = tr->import_copy_of(tv, PayloadPolicy::Copy);
4✔
4153
            q2 = tr->import_copy_of(q, PayloadPolicy::Copy);
4✔
4154
        }
4✔
4155
        {
4✔
4156
            auto tv3 = q2->find_all();
4✔
4157
            CHECK(tv2->is_in_sync());
4✔
4158
            if (detached == 0) {
4✔
4159
                CHECK_EQUAL(1, tv2->size());
2✔
4160
                CHECK_EQUAL(target, tv2->get_key(0));
2✔
4161
                CHECK_EQUAL(1, tv3.size());
2✔
4162
                CHECK_EQUAL(target, tv3.get_key(0));
2✔
4163
            }
2✔
4164
            else {
2✔
4165
                CHECK_EQUAL(0, tv2->size());
2✔
4166
                CHECK_EQUAL(0, tv3.size());
2✔
4167
            }
2✔
4168
            tr->close();
4✔
4169
        }
4✔
4170
    }
4✔
4171
}
2✔
4172

4173

4174
#ifdef LEGACY_TESTS // (not useful as std unittest)
4175
namespace {
4176

4177
void do_write_work(std::string path, size_t id, size_t num_rows)
4178
{
4179
    const size_t num_iterations = 5000000; // this makes it run for a loooong time
4180
    const size_t payload_length_small = 10;
4181
    const size_t payload_length_large = 5000;   // > 4096 == page_size
4182
    Random random(random_int<unsigned long>()); // Seed from slow global generator
4183
    const char* key = crypt_key(true);
4184
    for (size_t rep = 0; rep < num_iterations; ++rep) {
4185
        std::unique_ptr<Replication> hist(make_in_realm_history());
4186
        DBRef sg = DB::create(*hist, path, DBOptions(key));
4187

4188
        TransactionRef rt = sg->start_read() LangBindHelper::promote_to_write(sg);
4189
        Group& group = const_cast<Group&>(rt.get_group());
4190
        TableRef t = rt->get_table(0);
4191

4192
        for (size_t i = 0; i < num_rows; ++i) {
4193
            const size_t payload_length = i % 10 == 0 ? payload_length_large : payload_length_small;
4194
            const char payload_char = 'a' + static_cast<char>((id + rep + i) % 26);
4195
            std::string std_payload(payload_length, payload_char);
4196
            StringData payload(std_payload);
4197

4198
            t->set_int(0, i, payload.size());
4199
            t->set_string(1, i, StringData(std_payload.c_str(), 1));
4200
            t->set_string(2, i, payload);
4201
        }
4202
        LangBindHelper::commit_and_continue_as_read(sg);
4203
    }
4204
}
4205

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

4243
} // end anonymous namespace
4244

4245

4246
// The following test is long running to try to catch race conditions
4247
// in with many reader writer threads on an encrypted realm and it is
4248
// not suited to automated testing.
4249
TEST_IF(Thread_AsynchronousIODataConsistency, false)
4250
{
4251
    SHARED_GROUP_TEST_PATH(path);
4252
    const int num_writer_threads = 2;
4253
    const int num_reader_threads = 2;
4254
    const int num_rows = 200; // 2 + REALM_MAX_BPNODE_SIZE;
4255
    const char* key = crypt_key(true);
4256
    std::unique_ptr<Replication> hist(make_in_realm_history());
4257
    DBRef sg = DB::create(*hist, path, DBOptions(key));
4258
    {
4259
        WriteTransaction wt(sg);
4260
        Group& group = wt.get_group();
4261
        TableRef t = rt->add_table("class_Table_Emulation_Name");
4262
        // add a column for each thread to write to
4263
        t->add_column(type_Int, "count", true);
4264
        t->add_column(type_String, "char", true);
4265
        t->add_column(type_String, "payload", true);
4266
        t->add_empty_row(num_rows);
4267
        wt.commit();
4268
    }
4269

4270
    Thread writer_threads[num_writer_threads];
4271
    for (int i = 0; i < num_writer_threads; ++i) {
4272
        writer_threads[i].start(std::bind(do_write_work, std::string(path), i, num_rows));
4273
    }
4274
    Thread reader_threads[num_reader_threads];
4275
    for (int i = 0; i < num_reader_threads; ++i) {
4276
        reader_threads[i].start(std::bind(do_read_verify, std::string(path)));
4277
    }
4278
    for (int i = 0; i < num_writer_threads; ++i) {
4279
        writer_threads[i].join();
4280
    }
4281

4282
    {
4283
        WriteTransaction wt(sg);
4284
        Group& group = wt.get_group();
4285
        TableRef t = rt->get_table("class_Table_Emulation_Name");
4286
        t->set_string(1, 0, "stop reading");
4287
        wt.commit();
4288
    }
4289

4290
    for (int i = 0; i < num_reader_threads; ++i) {
4291
        reader_threads[i].join();
4292
    }
4293
}
4294
#endif
4295

4296

4297
TEST(LangBindHelper_HandoverTableRef)
4298
{
2✔
4299
    SHARED_GROUP_TEST_PATH(path);
2✔
4300
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4301
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4302
    TransactionRef reader;
2✔
4303
    TableRef table;
2✔
4304
    {
2✔
4305
        auto writer = sg->start_write();
2✔
4306
        TableRef table1 = writer->add_table("table1");
2✔
4307
        writer->commit_and_continue_as_read();
2✔
4308
        auto vid = writer->get_version_of_current_transaction();
2✔
4309
        reader = sg->start_read(vid);
2✔
4310
        table = reader->import_copy_of(table1);
2✔
4311
    }
2✔
4312
    CHECK(bool(table));
2✔
4313
    CHECK(table->size() == 0);
2✔
4314
}
2✔
4315

4316
TEST(LangBindHelper_HandoverLinkView)
4317
{
2✔
4318
    SHARED_GROUP_TEST_PATH(path);
2✔
4319
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4320
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4321
    TransactionRef reader;
2✔
4322
    ColKey col1;
2✔
4323

1✔
4324
    auto writer = sg->start_write();
2✔
4325

1✔
4326
    TableRef table1 = writer->add_table("table1");
2✔
4327
    TableRef table2 = writer->add_table("table2");
2✔
4328

1✔
4329
    // add some more columns to table1 and table2
1✔
4330
    col1 = table1->add_column(type_Int, "col1");
2✔
4331
    table1->add_column(type_String, "str1");
2✔
4332

1✔
4333
    // add some rows
1✔
4334
    auto to1 = table1->create_object().set_all(300, "delta");
2✔
4335
    auto to2 = table1->create_object().set_all(100, "alfa");
2✔
4336
    auto to3 = table1->create_object().set_all(200, "beta");
2✔
4337

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

1✔
4340
    auto o1 = table2->create_object();
2✔
4341
    table2->create_object();
2✔
4342
    LnkLstPtr lvr = o1.get_linklist_ptr(col_link2);
2✔
4343
    lvr->clear();
2✔
4344
    lvr->add(to1.get_key());
2✔
4345
    lvr->add(to2.get_key());
2✔
4346
    lvr->add(to3.get_key());
2✔
4347
    writer->commit_and_continue_as_read();
2✔
4348
    reader = writer->duplicate();
2✔
4349
    auto ll = reader->import_copy_of(lvr);
2✔
4350
    {
2✔
4351
        // validate inside reader transaction
1✔
4352
        // Return all rows of table1 (the linked-to-table) that match the criteria and is in the LinkList
1✔
4353

1✔
4354
        // q.m_table = table1
1✔
4355
        // q.m_view = lvr
1✔
4356
        TableRef table1b = reader->get_table("table1");
2✔
4357
        Query q = table1b->where(*ll).and_query(table1b->column<Int>(col1) > 100);
2✔
4358

1✔
4359
        // tv.m_table == table1
1✔
4360
        TableView tv = q.find_all(); // tv = { 0, 2 }
2✔
4361

1✔
4362

1✔
4363
        CHECK_EQUAL(2, tv.size());
2✔
4364
        CHECK_EQUAL(to1.get_key(), tv.get_key(0));
2✔
4365
        CHECK_EQUAL(to3.get_key(), tv.get_key(1));
2✔
4366
    }
2✔
4367
    {
2✔
4368
        // Change table1 and verify that the change does not propagate through the handed-over linkview
1✔
4369
        writer->promote_to_write();
2✔
4370
        to1.set<int64_t>(col1, 50);
2✔
4371
        writer->commit_and_continue_as_read();
2✔
4372
    }
2✔
4373
    {
2✔
4374
        TableRef table1b = reader->get_table("table1");
2✔
4375
        Query q = table1b->where(*ll).and_query(table1b->column<Int>(col1) > 100);
2✔
4376

1✔
4377
        // tv.m_table == table1
1✔
4378
        TableView tv = q.find_all(); // tv = { 0, 2 }
2✔
4379

1✔
4380

1✔
4381
        CHECK_EQUAL(2, tv.size());
2✔
4382
        CHECK_EQUAL(to1.get_key(), tv.get_key(0));
2✔
4383
        CHECK_EQUAL(to3.get_key(), tv.get_key(1));
2✔
4384
    }
2✔
4385
}
2✔
4386

4387
TEST(LangBindHelper_HandoverDistinctView)
4388
{
2✔
4389
    SHARED_GROUP_TEST_PATH(path);
2✔
4390
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4391
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4392
    TransactionRef reader;
2✔
4393
    std::unique_ptr<TableView> tv2;
2✔
4394
    Obj obj2b;
2✔
4395
    {
2✔
4396
        {
2✔
4397
            TableView tv1;
2✔
4398
            auto writer = sg->start_write();
2✔
4399
            TableRef table = writer->add_table("table2");
2✔
4400
            auto col = table->add_column(type_Int, "first");
2✔
4401
            auto obj1 = table->create_object().set_all(100);
2✔
4402
            table->create_object().set_all(100);
2✔
4403

1✔
4404
            writer->commit_and_continue_as_read();
2✔
4405
            tv1 = table->where().find_all();
2✔
4406
            tv1.distinct(col);
2✔
4407
            CHECK(tv1.size() == 1);
2✔
4408
            CHECK(tv1.get_key(0) == obj1.get_key());
2✔
4409
            CHECK(tv1.is_attached());
2✔
4410

1✔
4411
            reader = writer->duplicate();
2✔
4412
            tv2 = reader->import_copy_of(tv1, PayloadPolicy::Copy);
2✔
4413
            obj2b = reader->import_copy_of(obj1);
2✔
4414
            CHECK(tv1.is_attached());
2✔
4415
        }
2✔
4416
        {
2✔
4417
            // importing side: working in the context of "reader"
1✔
4418
            CHECK(tv2->is_in_sync());
2✔
4419
            CHECK(tv2->is_attached());
2✔
4420

1✔
4421
            CHECK_EQUAL(tv2->size(), 1);
2✔
4422
            CHECK_EQUAL(tv2->get_key(0), obj2b.get_key());
2✔
4423

1✔
4424
            // distinct property must remain through handover such that second row is kept being omitted
1✔
4425
            // after sync_if_needed()
1✔
4426
            tv2->sync_if_needed();
2✔
4427
            CHECK_EQUAL(tv2->size(), 1);
2✔
4428
            CHECK_EQUAL(tv2->get_key(0), obj2b.get_key());
2✔
4429
        }
2✔
4430
    }
2✔
4431
}
2✔
4432

4433

4434
TEST(LangBindHelper_HandoverWithReverseDependency)
4435
{
2✔
4436
    SHARED_GROUP_TEST_PATH(path);
2✔
4437
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4438
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4439
    auto trans = sg->start_read();
2✔
4440
    {
2✔
4441
        // Untyped interface
1✔
4442
        TableView tv1;
2✔
4443
        TableView tv2;
2✔
4444
        ColKey ck;
2✔
4445
        {
2✔
4446
            trans->promote_to_write();
2✔
4447
            TableRef table = trans->add_table("table2");
2✔
4448
            ck = table->add_column(type_Int, "first");
2✔
4449
            for (int i = 0; i < 100; ++i) {
202✔
4450
                table->create_object().set_all(i);
200✔
4451
            }
200✔
4452
            trans->commit_and_continue_as_read();
2✔
4453
            tv1 = table->where().find_all();
2✔
4454
            tv2 = table->where(&tv1).find_all();
2✔
4455
            CHECK(tv1.is_attached());
2✔
4456
            CHECK(tv2.is_attached());
2✔
4457
            CHECK_EQUAL(100, tv1.size());
2✔
4458
            for (int i = 0; i < 100; ++i)
202✔
4459
                CHECK_EQUAL(i, tv1.get_object(i).get<int64_t>(ck));
200✔
4460
            CHECK_EQUAL(100, tv2.size());
2✔
4461
            for (int i = 0; i < 100; ++i)
202✔
4462
                CHECK_EQUAL(i, tv1.get_object(i).get<int64_t>(ck));
200✔
4463
            auto dummy_trans = trans->duplicate();
2✔
4464
            auto dummy_tv = dummy_trans->import_copy_of(tv1, PayloadPolicy::Copy);
2✔
4465
            CHECK(tv1.is_attached());
2✔
4466
            CHECK(tv2.is_attached());
2✔
4467
        }
2✔
4468
    }
2✔
4469
}
2✔
4470

4471
TEST(LangBindHelper_HandoverTableViewFromBacklink)
4472
{
2✔
4473
    SHARED_GROUP_TEST_PATH(path);
2✔
4474
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4475
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4476
    auto group_w = sg->start_write();
2✔
4477

1✔
4478
    TableRef source = group_w->add_table("source");
2✔
4479
    source->add_column(type_Int, "int");
2✔
4480

1✔
4481
    TableRef links = group_w->add_table("links");
2✔
4482
    ColKey col = links->add_column(*source, "link");
2✔
4483

1✔
4484
    std::vector<ObjKey> dummies;
2✔
4485
    source->create_objects(100, dummies);
2✔
4486
    links->create_objects(100, dummies);
2✔
4487
    auto source_it = source->begin();
2✔
4488
    auto links_it = links->begin();
2✔
4489
    for (int i = 0; i < 100; ++i) {
202✔
4490
        auto obj = source_it->set_all(i);
200✔
4491
        links_it->set(col, obj.get_key());
200✔
4492
        ++source_it;
200✔
4493
        ++links_it;
200✔
4494
    }
200✔
4495
    group_w->commit_and_continue_as_read();
2✔
4496

1✔
4497
    for (int i = 0; i < 100; ++i) {
202✔
4498
        TableView tv = source->get_object(i).get_backlink_view(links, col);
200✔
4499
        CHECK(tv.is_attached());
200✔
4500
        CHECK_EQUAL(1, tv.size());
200✔
4501
        ObjKey o_key = source->get_object(i).get_key();
200✔
4502
        CHECK_EQUAL(o_key, tv.get_key(0));
200✔
4503
        auto group = group_w->duplicate();
200✔
4504
        auto tv2 = group->import_copy_of(tv, PayloadPolicy::Copy);
200✔
4505
        CHECK(tv.is_attached());
200✔
4506
        CHECK(tv2->is_attached());
200✔
4507
        CHECK_EQUAL(1, tv2->size());
200✔
4508
        CHECK_EQUAL(o_key, tv2->get_key(0));
200✔
4509
    }
200✔
4510
}
2✔
4511

4512
// Verify that handing over an out-of-sync TableView that represents backlinks
4513
// to a deleted row results in a TableView that can be brought back into sync.
4514
TEST(LangBindHelper_HandoverOutOfSyncTableViewFromBacklinksToDeletedRow)
4515
{
2✔
4516
    SHARED_GROUP_TEST_PATH(path);
2✔
4517
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4518
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4519
    auto group_w = sg->start_write();
2✔
4520

1✔
4521
    TableRef target = group_w->add_table("target");
2✔
4522
    target->add_column(type_Int, "int");
2✔
4523

1✔
4524
    TableRef links = group_w->add_table("links");
2✔
4525
    auto col = links->add_column(*target, "link");
2✔
4526

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

1✔
4529
    links->create_object().set_all(obj_t.get_key());
2✔
4530

1✔
4531
    TableView tv = obj_t.get_backlink_view(links, col);
2✔
4532
    CHECK_EQUAL(true, tv.is_attached());
2✔
4533
    CHECK_EQUAL(true, tv.is_in_sync());
2✔
4534
    CHECK_EQUAL(false, tv.depends_on_deleted_object());
2✔
4535
    CHECK_EQUAL(1, tv.size());
2✔
4536

1✔
4537
    // Bring the view out of sync, and have it depend on a deleted row.
1✔
4538
    target->remove_object(obj_t.get_key());
2✔
4539
    CHECK_EQUAL(true, tv.is_attached());
2✔
4540
    CHECK_EQUAL(false, tv.is_in_sync());
2✔
4541
    CHECK_EQUAL(true, tv.depends_on_deleted_object());
2✔
4542
    CHECK_EQUAL(1, tv.size());
2✔
4543
    tv.sync_if_needed();
2✔
4544
    CHECK_EQUAL(0, tv.size());
2✔
4545
    group_w->commit_and_continue_as_read();
2✔
4546
    auto group = group_w->duplicate();
2✔
4547
    auto tv2 = group->import_copy_of(tv, PayloadPolicy::Copy);
2✔
4548
    CHECK_EQUAL(true, tv2->depends_on_deleted_object());
2✔
4549
    CHECK_EQUAL(0, tv2->size());
2✔
4550
}
2✔
4551

4552
// Test that we can handover a query involving links, and that after the
4553
// handover export, the handover is completely decoupled from later changes
4554
// done on accessors belonging to the exporting shared group
4555
TEST(LangBindHelper_HandoverWithLinkQueries)
4556
{
2✔
4557
    SHARED_GROUP_TEST_PATH(path);
2✔
4558
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4559
    DBRef db = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4560
    auto group_w = db->start_write();
2✔
4561
    // First setup data so that we can do a query on links
1✔
4562
    TableRef table1 = group_w->add_table("table1");
2✔
4563
    TableRef table2 = group_w->add_table("table2");
2✔
4564
    // add some more columns to table1 and table2
1✔
4565
    table1->add_column(type_Int, "col1");
2✔
4566
    table1->add_column(type_String, "str1");
2✔
4567

1✔
4568
    table2->add_column(type_Int, "col1");
2✔
4569
    auto col_str = table2->add_column(type_String, "str2");
2✔
4570

1✔
4571
    // add some rows
1✔
4572
    auto o10 = table1->create_object().set_all(100, "foo");
2✔
4573
    auto o11 = table1->create_object().set_all(200, "!");
2✔
4574
    table1->create_object().set_all(300, "bar");
2✔
4575
    table2->create_object().set_all(400, "hello");
2✔
4576
    auto o21 = table2->create_object().set_all(500, "world");
2✔
4577
    auto o22 = table2->create_object().set_all(600, "!");
2✔
4578

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

1✔
4581
    // set some links
1✔
4582
    auto links1 = o10.get_linklist(col_link2);
2✔
4583
    CHECK(links1.is_attached());
2✔
4584
    links1.add(o21.get_key());
2✔
4585

1✔
4586
    auto links2 = o11.get_linklist(col_link2);
2✔
4587
    CHECK(links2.is_attached());
2✔
4588
    links2.add(o21.get_key());
2✔
4589
    links2.add(o22.get_key());
2✔
4590
    group_w->commit_and_continue_as_read();
2✔
4591

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

1✔
4598
    auto rec1 = group_w->duplicate();
2✔
4599
    auto q1 = rec1->import_copy_of(query, PayloadPolicy::Copy);
2✔
4600
    auto rec2 = group_w->duplicate();
2✔
4601
    auto q2 = rec2->import_copy_of(query, PayloadPolicy::Copy);
2✔
4602

1✔
4603
    {
2✔
4604
        realm::TableView tv = q1->find_all();
2✔
4605
        CHECK_EQUAL(0, tv.size());
2✔
4606
    }
2✔
4607

1✔
4608
    // On the exporting side, change the data such that the query will now have
1✔
4609
    // non-zero results if evaluated in that context.
1✔
4610
    group_w->promote_to_write();
2✔
4611
    auto o23 = table2->create_object().set_all(700, "nabil");
2✔
4612
    links1.add(o23.get_key());
2✔
4613
    group_w->commit_and_continue_as_read();
2✔
4614
    CHECK_EQUAL(1, query.count());
2✔
4615
    {
2✔
4616
        // Import query and evaluate in the old context. This should *not* be
1✔
4617
        // affected by the change done above on the exporting side.
1✔
4618
        realm::TableView tv2 = q2->find_all();
2✔
4619
        CHECK_EQUAL(0, tv2.size());
2✔
4620
    }
2✔
4621
}
2✔
4622

4623

4624
TEST(LangBindHelper_HandoverQueryLinksTo)
4625
{
2✔
4626
    SHARED_GROUP_TEST_PATH(path);
2✔
4627
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4628
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4629

1✔
4630
    TransactionRef reader;
2✔
4631
    std::unique_ptr<Query> query;
2✔
4632
    std::unique_ptr<Query> queryOr;
2✔
4633
    std::unique_ptr<Query> queryAnd;
2✔
4634
    std::unique_ptr<Query> queryNot;
2✔
4635
    std::unique_ptr<Query> queryAndAndOr;
2✔
4636
    std::unique_ptr<Query> queryWithExpression;
2✔
4637
    std::unique_ptr<Query> queryLinksToDetached;
2✔
4638
    {
2✔
4639
        auto group_w = sg->start_write();
2✔
4640
        TableRef source = group_w->add_table("source");
2✔
4641
        TableRef target = group_w->add_table("target");
2✔
4642

1✔
4643
        ColKey col_link = source->add_column(*target, "link");
2✔
4644
        ColKey col_name = target->add_column(type_String, "name");
2✔
4645

1✔
4646
        std::vector<ObjKey> keys;
2✔
4647
        target->create_objects(4, keys);
2✔
4648
        target->get_object(0).set(col_name, "A");
2✔
4649
        target->get_object(1).set(col_name, "B");
2✔
4650
        target->get_object(2).set(col_name, "C");
2✔
4651
        target->get_object(3).set(col_name, "D");
2✔
4652

1✔
4653
        source->create_object().set_all(keys[0]);
2✔
4654
        source->create_object().set_all(keys[1]);
2✔
4655
        source->create_object().set_all(keys[2]);
2✔
4656

1✔
4657
        Obj detached_row = target->get_object(3);
2✔
4658
        target->remove_object(detached_row.get_key());
2✔
4659

1✔
4660
        group_w->commit_and_continue_as_read();
2✔
4661

1✔
4662
        Query _query = source->column<Link>(col_link) == target->get_object(0);
2✔
4663
        Query _queryOr = source->column<Link>(col_link) == target->get_object(0) ||
2✔
4664
                         source->column<Link>(col_link) == target->get_object(1);
2✔
4665
        Query _queryAnd = source->column<Link>(col_link) == target->get_object(0) &&
2✔
4666
                          source->column<Link>(col_link) == target->get_object(0);
2✔
4667
        Query _queryNot = !(source->column<Link>(col_link) == target->get_object(0)) &&
2✔
4668
                          source->column<Link>(col_link) == target->get_object(1);
2✔
4669
        Query _queryAndAndOr = source->where().group().and_query(_queryOr).end_group().and_query(_queryAnd);
2✔
4670
        Query _queryWithExpression = source->column<Link>(col_link).is_not_null() && _query;
2✔
4671
        Query _queryLinksToDetached = source->where().links_to(col_link, detached_row.get_key());
2✔
4672

1✔
4673
        // handover:
1✔
4674
        reader = group_w->duplicate();
2✔
4675
        query = reader->import_copy_of(_query, PayloadPolicy::Copy);
2✔
4676
        queryOr = reader->import_copy_of(_queryOr, PayloadPolicy::Copy);
2✔
4677
        queryAnd = reader->import_copy_of(_queryAnd, PayloadPolicy::Copy);
2✔
4678
        queryNot = reader->import_copy_of(_queryNot, PayloadPolicy::Copy);
2✔
4679
        queryAndAndOr = reader->import_copy_of(_queryAndAndOr, PayloadPolicy::Copy);
2✔
4680
        queryWithExpression = reader->import_copy_of(_queryWithExpression, PayloadPolicy::Copy);
2✔
4681
        queryLinksToDetached = reader->import_copy_of(_queryLinksToDetached, PayloadPolicy::Copy);
2✔
4682

1✔
4683
        CHECK_EQUAL(1, _query.count());
2✔
4684
        CHECK_EQUAL(2, _queryOr.count());
2✔
4685
        CHECK_EQUAL(1, _queryAnd.count());
2✔
4686
        CHECK_EQUAL(1, _queryNot.count());
2✔
4687
        CHECK_EQUAL(1, _queryAndAndOr.count());
2✔
4688
        CHECK_EQUAL(1, _queryWithExpression.count());
2✔
4689
        CHECK_EQUAL(0, _queryLinksToDetached.count());
2✔
4690
    }
2✔
4691
    {
2✔
4692
        CHECK_EQUAL(1, query->count());
2✔
4693
        CHECK_EQUAL(2, queryOr->count());
2✔
4694
        CHECK_EQUAL(1, queryAnd->count());
2✔
4695
        CHECK_EQUAL(1, queryNot->count());
2✔
4696
        CHECK_EQUAL(1, queryAndAndOr->count());
2✔
4697
        CHECK_EQUAL(1, queryWithExpression->count());
2✔
4698
        CHECK_EQUAL(0, queryLinksToDetached->count());
2✔
4699

1✔
4700

1✔
4701
        // Remove the linked-to row.
1✔
4702
        {
2✔
4703
            auto group_w = sg->start_write();
2✔
4704
            TableRef target = group_w->get_table("target");
2✔
4705
            target->remove_object(target->begin()->get_key());
2✔
4706
            group_w->commit();
2✔
4707
        }
2✔
4708

1✔
4709
        // Verify that the queries against the read-only shared group gives the same results.
1✔
4710
        CHECK_EQUAL(1, query->count());
2✔
4711
        CHECK_EQUAL(2, queryOr->count());
2✔
4712
        CHECK_EQUAL(1, queryAnd->count());
2✔
4713
        CHECK_EQUAL(1, queryNot->count());
2✔
4714
        CHECK_EQUAL(1, queryAndAndOr->count());
2✔
4715
        CHECK_EQUAL(1, queryWithExpression->count());
2✔
4716
        CHECK_EQUAL(0, queryLinksToDetached->count());
2✔
4717
    }
2✔
4718
}
2✔
4719

4720

4721
TEST(LangBindHelper_HandoverQuerySubQuery)
4722
{
2✔
4723
    SHARED_GROUP_TEST_PATH(path);
2✔
4724
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4725
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4726

1✔
4727
    TransactionRef reader;
2✔
4728
    std::unique_ptr<Query> query;
2✔
4729
    {
2✔
4730
        auto group_w = sg->start_write();
2✔
4731

1✔
4732
        TableRef source = group_w->add_table("source");
2✔
4733
        TableRef target = group_w->add_table("target");
2✔
4734

1✔
4735
        ColKey col_link = source->add_column(*target, "link");
2✔
4736
        ColKey col_name = target->add_column(type_String, "name");
2✔
4737

1✔
4738
        std::vector<ObjKey> keys;
2✔
4739
        target->create_objects(3, keys);
2✔
4740
        target->get_object(keys[0]).set(col_name, "A");
2✔
4741
        target->get_object(keys[1]).set(col_name, "B");
2✔
4742
        target->get_object(keys[2]).set(col_name, "C");
2✔
4743

1✔
4744
        source->create_object().set_all(keys[0]);
2✔
4745
        source->create_object().set_all(keys[1]);
2✔
4746
        source->create_object().set_all(keys[2]);
2✔
4747

1✔
4748
        group_w->commit_and_continue_as_read();
2✔
4749

1✔
4750
        realm::Query query_2 = source->column<Link>(col_link, target->column<String>(col_name) == "C").count() == 1;
2✔
4751
        reader = group_w->duplicate();
2✔
4752
        query = reader->import_copy_of(query_2, PayloadPolicy::Copy);
2✔
4753
    }
2✔
4754

1✔
4755
    CHECK_EQUAL(1, query->count());
2✔
4756

1✔
4757
    // Remove the linked-to row.
1✔
4758
    {
2✔
4759
        auto group_w = sg->start_write();
2✔
4760

1✔
4761
        TableRef target = group_w->get_table("target");
2✔
4762
        target->clear();
2✔
4763
        group_w->commit_and_continue_as_read();
2✔
4764
    }
2✔
4765

1✔
4766
    // Verify that the queries against the read-only shared group gives the same results.
1✔
4767
    CHECK_EQUAL(1, query->count());
2✔
4768
}
2✔
4769

4770
TEST(LangBindHelper_VersionControl)
4771
{
2✔
4772
    Random random(random_int<unsigned long>());
2✔
4773

1✔
4774
    const int num_versions = 10;
2✔
4775
    const int num_random_tests = 100;
2✔
4776
    DB::VersionID versions[num_versions];
2✔
4777
    std::vector<TransactionRef> trs;
2✔
4778
    SHARED_GROUP_TEST_PATH(path);
2✔
4779
    {
2✔
4780
        // Create a new shared db
1✔
4781
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4782
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4783
        // first create 'num_version' versions
1✔
4784
        ColKey col;
2✔
4785
        auto reader = sg->start_read();
2✔
4786
        {
2✔
4787
            WriteTransaction wt(sg);
2✔
4788
            col = wt.get_or_add_table("test")->add_column(type_Int, "a");
2✔
4789
            wt.commit();
2✔
4790
        }
2✔
4791
        for (int i = 0; i < num_versions; ++i) {
22✔
4792
            {
20✔
4793
                WriteTransaction wt(sg);
20✔
4794
                auto t = wt.get_table("test");
20✔
4795
                t->create_object().set_all(i);
20✔
4796
                wt.commit();
20✔
4797
            }
20✔
4798
            {
20✔
4799
                auto rt = sg->start_read();
20✔
4800
                trs.push_back(rt->duplicate());
20✔
4801
                versions[i] = rt->get_version_of_current_transaction();
20✔
4802
            }
20✔
4803
        }
20✔
4804

1✔
4805
        // do steps of increasing size from the first version to the last,
1✔
4806
        // including a "step on the spot" (from version 0 to 0)
1✔
4807
        {
2✔
4808
            for (int k = 0; k < num_versions; ++k) {
22✔
4809
                // std::cerr << "Advancing from initial version to version " << k << std::endl;
10✔
4810
                auto g = sg->start_read(versions[0]);
20✔
4811
                auto t = g->get_table("test");
20✔
4812
                CHECK(versions[k] >= versions[0]);
20✔
4813
                g->verify();
20✔
4814
                g->advance_read(versions[k]);
20✔
4815
                g->verify();
20✔
4816
                auto o = *(t->begin() + k);
20✔
4817
                CHECK_EQUAL(k, o.get<int64_t>(col));
20✔
4818
            }
20✔
4819
        }
2✔
4820

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

10✔
4825
            auto g = sg->start_read(versions[i]);
20✔
4826
            g->verify();
20✔
4827
            auto t = g->get_table("test");
20✔
4828
            auto o = *(t->begin() + i);
20✔
4829
            CHECK_EQUAL(i, o.get<int64_t>(col));
20✔
4830
        }
20✔
4831

1✔
4832
        // then advance through the versions going forward
1✔
4833
        {
2✔
4834
            auto g = sg->start_read(versions[0]);
2✔
4835
            g->verify();
2✔
4836
            auto t = g->get_table("test");
2✔
4837
            for (int k = 0; k < num_versions; ++k) {
22✔
4838
                // std::cerr << "Advancing to version " << k << std::endl;
10✔
4839
                CHECK(k == 0 || versions[k] >= versions[k - 1]);
20✔
4840

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

1✔
4886
        // Validate that all the versions are now unreachable
1✔
4887
        for (int i = 0; i < num_versions; ++i)
22✔
4888
            CHECK_THROW(sg->start_read(versions[i]), DB::BadVersion);
20✔
4889
    }
2✔
4890
}
2✔
4891

4892
TEST(LangBindHelper_RollbackToInitialState1)
4893
{
2✔
4894
    SHARED_GROUP_TEST_PATH(path);
2✔
4895
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
4896
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
4897
    auto trans = sg_w->start_read();
2✔
4898
    trans->promote_to_write();
2✔
4899
    trans->rollback_and_continue_as_read();
2✔
4900
}
2✔
4901

4902

4903
TEST(LangBindHelper_RollbackToInitialState2)
4904
{
2✔
4905
    SHARED_GROUP_TEST_PATH(path);
2✔
4906
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
4907
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
4908
    auto trans = sg_w->start_write();
2✔
4909
    trans->rollback();
2✔
4910
}
2✔
4911

4912
// non-concurrent because we test the filesystem which may
4913
// be used by other tests at the same time otherwise
4914
NONCONCURRENT_TEST(LangBindHelper_Compact)
4915
{
2✔
4916
    SHARED_GROUP_TEST_PATH(path);
2✔
4917
    size_t N = 100;
2✔
4918
    std::string dir_path = File::parent_dir(path);
2✔
4919
    dir_path = dir_path.empty() ? "." : dir_path;
2✔
4920
    auto dir_has_tmp_compaction = [&dir_path]() -> size_t {
6✔
4921
        DirScanner dir(dir_path);
6✔
4922
        std::string name;
6✔
4923
        while (dir.next(name)) {
288✔
4924
            if (name.find("tmp_compaction_space") != std::string::npos) {
282✔
4925
                return true;
×
4926
            }
×
4927
        }
282✔
4928
        return false;
6✔
4929
    };
6✔
4930

1✔
4931
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4932
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
4933
    {
2✔
4934
        WriteTransaction w(sg);
2✔
4935
        TableRef table = w.get_or_add_table("test");
2✔
4936
        table->add_column(type_Int, "int");
2✔
4937
        for (size_t i = 0; i < N; ++i) {
202✔
4938
            table->create_object().set_all(static_cast<signed>(i));
200✔
4939
        }
200✔
4940
        w.commit();
2✔
4941
    }
2✔
4942
    {
2✔
4943
        ReadTransaction r(sg);
2✔
4944
        ConstTableRef table = r.get_table("test");
2✔
4945
        CHECK_EQUAL(N, table->size());
2✔
4946
        CHECK(File::exists(dir_path));
2✔
4947
        CHECK(File::is_dir(dir_path));
2✔
4948
        CHECK(!dir_has_tmp_compaction());
2✔
4949
    }
2✔
4950
    {
2✔
4951
        CHECK_EQUAL(true, sg->compact());
2✔
4952
        CHECK(!dir_has_tmp_compaction());
2✔
4953
    }
2✔
4954
    {
2✔
4955
        ReadTransaction r(sg);
2✔
4956
        ConstTableRef table = r.get_table("test");
2✔
4957
        CHECK_EQUAL(N, table->size());
2✔
4958
    }
2✔
4959
    {
2✔
4960
        WriteTransaction w(sg);
2✔
4961
        TableRef table = w.get_or_add_table("test");
2✔
4962
        table->create_object().set_all(0);
2✔
4963
        w.commit();
2✔
4964
    }
2✔
4965
    {
2✔
4966
        CHECK_EQUAL(true, sg->compact());
2✔
4967
        CHECK(!dir_has_tmp_compaction());
2✔
4968
    }
2✔
4969
}
2✔
4970

4971
TEST(LangBindHelper_CompactLargeEncryptedFile)
4972
{
2✔
4973
    SHARED_GROUP_TEST_PATH(path);
2✔
4974

1✔
4975
    std::vector<char> data(realm::util::page_size());
2✔
4976
    const size_t N = 32;
2✔
4977

1✔
4978
    {
2✔
4979
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4980
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
4981
        WriteTransaction wt(sg);
2✔
4982
        TableRef table = wt.get_or_add_table("test");
2✔
4983
        table->add_column(type_String, "string");
2✔
4984
        for (size_t i = 0; i < N; ++i) {
66✔
4985
            table->create_object().set_all(StringData(data.data(), data.size()));
64✔
4986
        }
64✔
4987
        wt.commit();
2✔
4988

1✔
4989
        CHECK_EQUAL(true, sg->compact());
2✔
4990

1✔
4991
        sg->close();
2✔
4992
    }
2✔
4993

1✔
4994
    {
2✔
4995
        std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
4996
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
4997
        ReadTransaction r(sg);
2✔
4998
        ConstTableRef table = r.get_table("test");
2✔
4999
        CHECK_EQUAL(N, table->size());
2✔
5000
    }
2✔
5001
}
2✔
5002

5003
TEST(LangBindHelper_CloseDBvsTransactions)
5004
{
2✔
5005
    SHARED_GROUP_TEST_PATH(path);
2✔
5006
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5007
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key(true)));
2✔
5008
    auto tr0 = sg->start_read();
2✔
5009
    auto tr1 = sg->start_write();
2✔
5010
    CHECK(tr1->add_table("possible"));
2✔
5011
    // write transactions must be closed (one way or the other) before DB::close
1✔
5012
    CHECK_THROW(sg->close(), LogicError);
2✔
5013
    tr1->rollback();
2✔
5014
    // closing the DB explicitly while there are open read transactions will fail
1✔
5015
    CHECK_THROW(sg->close(), LogicError);
2✔
5016
    // unless we explicitly ask for it to succeed()
1✔
5017
    sg->close(true);
2✔
5018
    CHECK(!sg->is_attached());
2✔
5019
    CHECK(!tr0->is_attached());
2✔
5020
    CHECK(!tr1->is_attached());
2✔
5021
    CHECK_THROW(sg->start_read(), LogicError);
2✔
5022
}
2✔
5023

5024
TEST(LangBindHelper_TableViewAggregateAfterAdvanceRead)
5025
{
2✔
5026
    SHARED_GROUP_TEST_PATH(path);
2✔
5027

1✔
5028
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5029
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5030
    ColKey col;
2✔
5031
    {
2✔
5032
        WriteTransaction w(sg_w);
2✔
5033
        TableRef table = w.add_table("test");
2✔
5034
        col = table->add_column(type_Double, "double");
2✔
5035
        table->create_object().set_all(1234.0);
2✔
5036
        table->create_object().set_all(-5678.0);
2✔
5037
        table->create_object().set_all(1000.0);
2✔
5038
        w.commit();
2✔
5039
    }
2✔
5040

1✔
5041
    auto reader = sg_w->start_read();
2✔
5042
    auto table_r = reader->get_table("test");
2✔
5043

1✔
5044
    // Create a table view with all refs detached.
1✔
5045
    TableView view = table_r->where().find_all();
2✔
5046
    {
2✔
5047
        WriteTransaction w(sg_w);
2✔
5048
        w.get_table("test")->clear();
2✔
5049
        w.commit();
2✔
5050
    }
2✔
5051
    reader->advance_read();
2✔
5052

1✔
5053
    // Verify that an aggregate on the view with detached refs gives the expected result.
1✔
5054
    CHECK_EQUAL(false, view.is_in_sync());
2✔
5055
    ObjKey res;
2✔
5056
    CHECK(view.min(col, &res)->is_null());
2✔
5057
    CHECK_EQUAL(ObjKey(), res);
2✔
5058

1✔
5059
    // Sync the view to discard the detached refs.
1✔
5060
    view.sync_if_needed();
2✔
5061

1✔
5062
    // Verify that an aggregate on the view still gives the expected result.
1✔
5063
    res = ObjKey();
2✔
5064
    CHECK(view.min(col, &res)->is_null());
2✔
5065
    CHECK_EQUAL(ObjKey(), res);
2✔
5066
}
2✔
5067

5068
// Tests handover of a Query. Especially it tests if next-gen-syntax nodes are deep copied correctly by
5069
// executing an imported query multiple times in parallel
5070
TEST_IF(LangBindHelper_HandoverFuzzyTest, TEST_DURATION > 0)
5071
{
×
5072
    SHARED_GROUP_TEST_PATH(path);
×
5073

5074
    const size_t threads = 5;
×
5075

5076
    size_t numberOfOwner = 100;
×
5077
    size_t numberOfDogsPerOwner = 20;
×
5078

5079
    std::atomic<bool> end_signal(false);
×
5080
    std::unique_ptr<Replication> hist(make_in_realm_history());
×
5081
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
×
5082

5083
    std::vector<TransactionRef> vids;
×
5084
    std::vector<std::unique_ptr<Query>> qs;
×
5085
    std::mutex vector_mutex;
×
5086

5087
    ColKey c0, c1, c2, c3;
×
5088
    {
×
5089
        auto rt = sg->start_write();
×
5090

5091
        TableRef owner = rt->add_table("Owner");
×
5092
        TableRef dog = rt->add_table("Dog");
×
5093

5094
        c0 = owner->add_column(type_String, "name");
×
5095
        c1 = owner->add_column_list(*dog, "link");
×
5096

5097
        c2 = dog->add_column(type_String, "name");
×
5098
        c3 = dog->add_column(*owner, "link");
×
5099

5100
        for (size_t i = 0; i < numberOfOwner; i++) {
×
5101

5102
            auto o = owner->create_object();
×
5103
            std::string owner_str(std::string("owner") + to_string(i));
×
5104
            o.set<StringData>(c0, owner_str);
×
5105

5106
            for (size_t j = 0; j < numberOfDogsPerOwner; j++) {
×
5107
                auto o_d = dog->create_object();
×
5108
                std::string dog_str(std::string("dog") + to_string(i * numberOfOwner + j));
×
5109
                o_d.set<StringData>(c2, dog_str);
×
5110
                o_d.set(c3, o.get_key());
×
5111
                auto ll = o.get_linklist(c1);
×
5112
                ll.add(o_d.get_key());
×
5113
            }
×
5114
        }
×
5115
        rt->verify();
×
5116
        {
×
5117
            realm::Query query = dog->link(c3).column<String>(c0) == "owner" + to_string(rand() % numberOfOwner);
×
5118
            query.find_all(); // <-- fails
×
5119
        }
×
5120
        rt->commit();
×
5121
    }
×
5122

5123
    auto async = [&]() {
×
5124
        // Async thread
5125
        //************************************************************************************************
5126
        while (!end_signal) {
×
5127
            millisleep(10);
×
5128

5129
            vector_mutex.lock();
×
5130
            if (qs.size() > 0) {
×
5131

5132
                auto t = vids[0];
×
5133
                vids.erase(vids.begin());
×
5134
                auto q = std::move(qs[0]);
×
5135
                qs.erase(qs.begin());
×
5136
                vector_mutex.unlock();
×
5137

5138
                realm::TableView tv = q->find_all();
×
5139
            }
×
5140
            else {
×
5141
                vector_mutex.unlock();
×
5142
            }
×
5143
        }
×
5144
        //************************************************************************************************
5145
    };
×
5146

5147
    auto rt = sg->start_read();
×
5148
    // Create and export query
5149
    TableRef dog = rt->get_table("Dog");
×
5150

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

5154
    Thread slaves[threads];
×
5155
    for (int i = 0; i != threads; ++i) {
×
5156
        slaves[i].start([=] {
×
5157
            async();
×
5158
        });
×
5159
    }
×
5160

5161
    // Main thread
5162
    //************************************************************************************************
5163
    for (size_t iter = 0; iter < 20 + TEST_DURATION * TEST_DURATION * 500; iter++) {
×
5164
        vector_mutex.lock();
×
5165
        rt->promote_to_write();
×
5166
        rt->commit_and_continue_as_read();
×
5167
        if (qs.size() < 100) {
×
5168
            for (size_t t = 0; t < 5; t++) {
×
5169
                auto t2 = rt->duplicate();
×
5170
                qs.push_back(t2->import_copy_of(query, PayloadPolicy::Move));
×
5171
                vids.push_back(t2);
×
5172
            }
×
5173
        }
×
5174
        vector_mutex.unlock();
×
5175

5176
        millisleep(100);
×
5177
    }
×
5178
    //************************************************************************************************
5179

5180
    end_signal = true;
×
5181
    for (int i = 0; i != threads; ++i)
×
5182
        slaves[i].join();
×
5183
}
×
5184

5185

5186
// TableView::clear() was originally reported to be slow when table was indexed and had links, but performance
5187
// has now doubled. This test is just a short sanity test that clear() still works.
5188
TEST(LangBindHelper_TableViewClear)
5189
{
2✔
5190
    SHARED_GROUP_TEST_PATH(path);
2✔
5191

1✔
5192
    int64_t number_of_history = 1000;
2✔
5193
    int64_t number_of_line = 18;
2✔
5194

1✔
5195
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5196
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5197
    TransactionRef tr;
2✔
5198
    ColKey col0, col1, col2, colA, colB;
2✔
5199
    // set up tables:
1✔
5200
    // history : ["id" (int), "parent" (int), "lines" (list(line))]
1✔
5201
    // line    : ["id" (int), "parent" (int)]
1✔
5202
    {
2✔
5203
        tr = sg->start_write();
2✔
5204

1✔
5205
        TableRef history = tr->add_table("history");
2✔
5206
        TableRef line = tr->add_table("line");
2✔
5207

1✔
5208
        col0 = history->add_column(type_Int, "id");
2✔
5209
        col1 = history->add_column(type_Int, "parent");
2✔
5210
        col2 = history->add_column_list(*line, "lines");
2✔
5211
        history->add_search_index(col1);
2✔
5212

1✔
5213
        colA = line->add_column(type_Int, "id");
2✔
5214
        colB = line->add_column(type_Int, "parent");
2✔
5215
        line->add_search_index(colB);
2✔
5216
        tr->commit_and_continue_as_read();
2✔
5217
    }
2✔
5218

1✔
5219
    {
2✔
5220
        tr->promote_to_write();
2✔
5221

1✔
5222
        TableRef history = tr->get_table("history");
2✔
5223
        TableRef line = tr->get_table("line");
2✔
5224

1✔
5225
        auto obj = history->create_object();
2✔
5226
        obj.set(col0, 1);
2✔
5227
        auto ll = obj.get_linklist(col2);
2✔
5228
        for (int64_t j = 0; j < number_of_line; ++j) {
38✔
5229
            Obj o = line->create_object().set_all(j, 0);
36✔
5230
            ll.add(o.get_key());
36✔
5231
        }
36✔
5232

1✔
5233
        for (int64_t i = 1; i < number_of_history; ++i) {
2,000✔
5234
            history->create_object().set_all(i, i + 1);
1,998✔
5235
            int64_t rj = i * number_of_line;
1,998✔
5236
            for (int64_t j = 1; j <= number_of_line; ++j) {
37,962✔
5237
                line->create_object().set_all(rj, j);
35,964✔
5238
                ++rj;
35,964✔
5239
            }
35,964✔
5240
        }
1,998✔
5241
        tr->commit_and_continue_as_read();
2✔
5242
        CHECK_EQUAL(number_of_history, history->size());
2✔
5243
        CHECK_EQUAL(number_of_history * number_of_line, line->size());
2✔
5244
    }
2✔
5245

1✔
5246
    // query and delete
1✔
5247
    {
2✔
5248
        tr->promote_to_write();
2✔
5249

1✔
5250
        TableRef line = tr->get_table("line");
2✔
5251

1✔
5252
        //    number_of_line = 2;
1✔
5253
        for (int64_t i = 1; i <= number_of_line; ++i) {
38✔
5254
            TableView tv = (line->column<Int>(colB) == i).find_all();
36✔
5255
            tv.clear();
36✔
5256
        }
36✔
5257
        tr->commit_and_continue_as_read();
2✔
5258
    }
2✔
5259

1✔
5260
    {
2✔
5261
        TableRef history = tr->get_table("history");
2✔
5262
        TableRef line = tr->get_table("line");
2✔
5263

1✔
5264
        CHECK_EQUAL(number_of_history, history->size());
2✔
5265
        CHECK_EQUAL(number_of_line, line->size());
2✔
5266
    }
2✔
5267
}
2✔
5268

5269

5270
TEST(LangBindHelper_SessionHistoryConsistency)
5271
{
2✔
5272
    // Check that we can reliably detect inconsist history
1✔
5273
    // types across concurrent session participants.
1✔
5274

1✔
5275
    // Errors of this kind are considered as incorrect API usage, and will lead
1✔
5276
    // to throwing of LogicError exceptions.
1✔
5277

1✔
5278
    SHARED_GROUP_TEST_PATH(path);
2✔
5279

1✔
5280
    // When starting with an empty Realm, all history types are allowed, but all
1✔
5281
    // session participants must still agree
1✔
5282
    {
2✔
5283
        // No history
1✔
5284
        DBRef sg = DB::create(path, false, DBOptions(crypt_key()));
2✔
5285

1✔
5286
        // Out-of-Realm history
1✔
5287
        std::unique_ptr<Replication> hist = realm::make_in_realm_history();
2✔
5288
        CHECK_RUNTIME_ERROR(DB::create(*hist, path, DBOptions(crypt_key())), ErrorCodes::IncompatibleSession);
2✔
5289
    }
2✔
5290
}
2✔
5291

5292

5293
TEST(LangBindHelper_InRealmHistory_Upgrade)
5294
{
2✔
5295
    SHARED_GROUP_TEST_PATH(path_1);
2✔
5296
    {
2✔
5297
        // Out-of-Realm history
1✔
5298
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5299
        DBRef sg = DB::create(*hist, path_1, DBOptions(crypt_key()));
2✔
5300
        WriteTransaction wt(sg);
2✔
5301
        wt.commit();
2✔
5302
    }
2✔
5303
    {
2✔
5304
        // In-Realm history
1✔
5305
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5306
        DBRef sg = DB::create(*hist, path_1, DBOptions(crypt_key()));
2✔
5307
        WriteTransaction wt(sg);
2✔
5308
        wt.commit();
2✔
5309
    }
2✔
5310
    SHARED_GROUP_TEST_PATH(path_2);
2✔
5311
    {
2✔
5312
        // No history
1✔
5313
        DBRef sg = DB::create(path_2, false, DBOptions(crypt_key()));
2✔
5314
        WriteTransaction wt(sg);
2✔
5315
        wt.commit();
2✔
5316
    }
2✔
5317
    {
2✔
5318
        // In-Realm history
1✔
5319
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5320
        DBRef sg = DB::create(*hist, path_2, DBOptions(crypt_key()));
2✔
5321
        WriteTransaction wt(sg);
2✔
5322
        wt.commit();
2✔
5323
    }
2✔
5324
}
2✔
5325

5326
TEST(LangBindHelper_InRealmHistory_Downgrade)
5327
{
2✔
5328
    SHARED_GROUP_TEST_PATH(path);
2✔
5329
    {
2✔
5330
        // In-Realm history
1✔
5331
        std::unique_ptr<Replication> hist = make_in_realm_history();
2✔
5332
        DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5333
        WriteTransaction wt(sg);
2✔
5334
        wt.commit();
2✔
5335
    }
2✔
5336
    {
2✔
5337
        // No history
1✔
5338
        CHECK_THROW(DB::create(path, false, DBOptions(crypt_key())), IncompatibleHistories);
2✔
5339
    }
2✔
5340
}
2✔
5341

5342
// Trigger erase_rows with num_rows == 0 by inserting zero rows
5343
// and then rolling back the transaction. There was a problem
5344
// where accessors were not updated correctly in this case because
5345
// of an early out when num_rows_to_erase is zero.
5346
TEST(LangBindHelper_RollbackInsertZeroRows)
5347
{
2✔
5348
    SHARED_GROUP_TEST_PATH(path)
2✔
5349
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5350
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5351
    auto g = sg->start_write();
2✔
5352

1✔
5353
    auto t0 = g->add_table("t0");
2✔
5354
    auto t1 = g->add_table("t1");
2✔
5355

1✔
5356
    auto col = t0->add_column(*t1, "t0_link_to_t1");
2✔
5357
    t0->create_object();
2✔
5358
    auto o1 = t0->create_object();
2✔
5359
    t1->create_object();
2✔
5360
    auto v1 = t1->create_object();
2✔
5361
    o1.set(col, v1.get_key());
2✔
5362

1✔
5363
    CHECK_EQUAL(t0->size(), 2);
2✔
5364
    CHECK_EQUAL(t1->size(), 2);
2✔
5365
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5366

1✔
5367
    g->commit_and_continue_as_read();
2✔
5368
    g->promote_to_write();
2✔
5369

1✔
5370
    std::vector<ObjKey> keys;
2✔
5371
    t1->create_objects(0, keys); // Insert zero rows
2✔
5372

1✔
5373
    CHECK_EQUAL(t0->size(), 2);
2✔
5374
    CHECK_EQUAL(t1->size(), 2);
2✔
5375
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5376

1✔
5377
    g->rollback_and_continue_as_read();
2✔
5378
    g->verify();
2✔
5379

1✔
5380
    CHECK_EQUAL(t0->size(), 2);
2✔
5381
    CHECK_EQUAL(t1->size(), 2);
2✔
5382
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5383
}
2✔
5384

5385

5386
TEST(LangBindHelper_RollbackRemoveZeroRows)
5387
{
2✔
5388
    SHARED_GROUP_TEST_PATH(path)
2✔
5389
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5390
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
2✔
5391
    auto g = sg->start_write();
2✔
5392

1✔
5393
    auto t0 = g->add_table("t0");
2✔
5394
    auto t1 = g->add_table("t1");
2✔
5395

1✔
5396
    auto col = t0->add_column(*t1, "t0_link_to_t1");
2✔
5397
    t0->create_object();
2✔
5398
    auto o1 = t0->create_object();
2✔
5399
    t1->create_object();
2✔
5400
    auto v1 = t1->create_object();
2✔
5401
    o1.set(col, v1.get_key());
2✔
5402

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

1✔
5407
    g->commit_and_continue_as_read();
2✔
5408
    g->promote_to_write();
2✔
5409

1✔
5410
    t1->clear();
2✔
5411

1✔
5412
    CHECK_EQUAL(t0->size(), 2);
2✔
5413
    CHECK_EQUAL(t1->size(), 0);
2✔
5414
    CHECK_EQUAL(o1.get<ObjKey>(col), ObjKey());
2✔
5415

1✔
5416
    g->rollback_and_continue_as_read();
2✔
5417
    g->verify();
2✔
5418

1✔
5419
    CHECK_EQUAL(t0->size(), 2);
2✔
5420
    CHECK_EQUAL(t1->size(), 2);
2✔
5421
    CHECK_EQUAL(o1.get<ObjKey>(col), v1.get_key());
2✔
5422
}
2✔
5423

5424
// Bug found by AFL during development of TimestampColumn
5425
TEST_TYPES(LangBindHelper_AddEmptyRowsAndRollBackTimestamp, std::true_type, std::false_type)
5426
{
4✔
5427
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5428
    SHARED_GROUP_TEST_PATH(path);
4✔
5429
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5430
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5431
    auto g = sg_w->start_write();
4✔
5432
    TableRef t = g->add_table("");
4✔
5433
    t->add_column(type_Int, "", nullable_toggle);
4✔
5434
    t->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5435
    g->commit_and_continue_as_read();
4✔
5436
    g->promote_to_write();
4✔
5437
    std::vector<ObjKey> keys;
4✔
5438
    t->create_objects(224, keys);
4✔
5439
    g->rollback_and_continue_as_read();
4✔
5440
    g->verify();
4✔
5441
}
4✔
5442

5443
// Another bug found by AFL during development of TimestampColumn
5444
TEST_TYPES(LangBindHelper_EmptyWrites, std::true_type, std::false_type)
5445
{
4✔
5446
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5447
    SHARED_GROUP_TEST_PATH(path);
4✔
5448
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5449
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5450
    auto g = sg_w->start_write();
4✔
5451
    TableRef t = g->add_table("");
4✔
5452
    t->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5453

2✔
5454
    for (int i = 0; i < 27; ++i) {
112✔
5455
        g->commit_and_continue_as_read();
108✔
5456
        g->promote_to_write();
108✔
5457
    }
108✔
5458

2✔
5459
    t->create_object();
4✔
5460
}
4✔
5461

5462

5463
// Found by AFL
5464
TEST_TYPES(LangBindHelper_SetTimestampRollback, std::true_type, std::false_type)
5465
{
4✔
5466
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5467
    SHARED_GROUP_TEST_PATH(path);
4✔
5468
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
4✔
5469
    DBRef sg = DB::create(*hist_w, path, DBOptions(crypt_key()));
4✔
5470
    auto g = sg->start_write();
4✔
5471
    auto table = g->add_table("");
4✔
5472
    table->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5473
    table->create_object().set_all(Timestamp(-1, -1));
4✔
5474
    g->rollback_and_continue_as_read();
4✔
5475
    g->verify();
4✔
5476
}
4✔
5477

5478

5479
// Found by AFL, probably related to the rollback version above
5480
TEST_TYPES(LangBindHelper_SetTimestampAdvanceRead, std::true_type, std::false_type)
5481
{
4✔
5482
    constexpr bool nullable_toggle = TEST_TYPE::value;
4✔
5483
    SHARED_GROUP_TEST_PATH(path);
4✔
5484
    std::unique_ptr<Replication> hist(make_in_realm_history());
4✔
5485
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
4✔
5486
    auto g_r = sg->start_read();
4✔
5487
    auto g_w = sg->start_write();
4✔
5488
    auto table = g_w->add_table("");
4✔
5489
    table->add_column(type_Timestamp, "gnyf", nullable_toggle);
4✔
5490
    table->create_object().set_all(Timestamp(-1, -1));
4✔
5491
    g_w->commit_and_continue_as_read();
4✔
5492
    g_w->verify();
4✔
5493
    g_r->advance_read();
4✔
5494
    g_r->verify();
4✔
5495
}
4✔
5496

5497

5498
// Found by AFL.
5499
TEST(LangbindHelper_BoolSearchIndexCommitPromote)
5500
{
2✔
5501
    SHARED_GROUP_TEST_PATH(path);
2✔
5502
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5503
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5504
    auto g = sg->start_write();
2✔
5505
    auto t = g->add_table("");
2✔
5506
    auto col = t->add_column(type_Bool, "gnyf", true);
2✔
5507
    std::vector<ObjKey> keys;
2✔
5508
    t->create_objects(5, keys);
2✔
5509
    t->get_object(keys[0]).set(col, false);
2✔
5510
    t->add_search_index(col);
2✔
5511
    g->commit_and_continue_as_read();
2✔
5512
    g->promote_to_write();
2✔
5513
    t->create_objects(5, keys);
2✔
5514
    t->remove_object(keys[8]);
2✔
5515
}
2✔
5516

5517

5518
// Found by AFL.
5519
TEST(LangbindHelper_GroupWriter_EdgeCaseAssert)
5520
{
2✔
5521
    SHARED_GROUP_TEST_PATH(path);
2✔
5522
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5523
    DBRef sg = DB::create(*hist, path, DBOptions(crypt_key()));
2✔
5524
    auto g_r = sg->start_read();
2✔
5525
    auto g_w = sg->start_write();
2✔
5526

1✔
5527
    auto t1 = g_w->add_table("dgrpnpgmjbchktdgagmqlihjckcdhpjccsjhnqlcjnbterse");
2✔
5528
    auto t2 = g_w->add_table("pknglaqnckqbffehqfgjnrepcfohoedkhiqsiedlotmaqitm");
2✔
5529
    t1->add_column(type_Double, "ggotpkoshbrcrmmqbagbfjetajlrrlbpjhhqrngfgdteilmj", true);
2✔
5530
    t2->add_column_list(*t1, "dtkiipajqdsfglbptieibknaoeeohqdlhftqmlriphobspjr");
2✔
5531
    std::vector<ObjKey> keys;
2✔
5532
    t1->create_objects(375, keys);
2✔
5533
    g_w->add_table("pnsidlijqeddnsgaesiijrrqedkdktmfekftogjccerhpeil");
2✔
5534
    g_r->close();
2✔
5535
    g_w->commit();
2✔
5536
    REALM_ASSERT_RELEASE(sg->compact());
2✔
5537
    g_w = sg->start_write();
2✔
5538
    g_r = sg->start_read();
2✔
5539
    g_r->verify();
2✔
5540
    g_w->add_table("citdgiaclkfbbksfaqegcfiqcserceaqmttkilnlbknoadtb");
2✔
5541
    g_w->add_table("tqtnnikpggeakeqcqhfqtshmimtjqkchgbnmbpttbetlahfi");
2✔
5542
    g_w->add_table("hkesaecjqbkemmmkffctacsnskekjbtqmpoetjnqkpactenf");
2✔
5543
    g_r->close();
2✔
5544
    g_w->commit();
2✔
5545
}
2✔
5546

5547
TEST(LangBindHelper_Bug2321)
5548
{
2✔
5549
    SHARED_GROUP_TEST_PATH(path);
2✔
5550
    ShortCircuitHistory hist;
2✔
5551
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
5552
    int i;
2✔
5553
    std::vector<ObjKey> target_keys;
2✔
5554
    std::vector<ObjKey> origin_keys;
2✔
5555
    ColKey col;
2✔
5556
    {
2✔
5557
        WriteTransaction wt(sg);
2✔
5558
        Group& group = wt.get_group();
2✔
5559
        TableRef target = group.add_table("target");
2✔
5560
        target->add_column(type_Int, "data");
2✔
5561
        target->create_objects(REALM_MAX_BPNODE_SIZE + 2, target_keys);
2✔
5562
        TableRef origin = group.add_table("origin");
2✔
5563
        col = origin->add_column_list(*target, "_link");
2✔
5564
        origin->create_objects(2, origin_keys);
2✔
5565
        wt.commit();
2✔
5566
    }
2✔
5567

1✔
5568
    {
2✔
5569
        WriteTransaction wt(sg);
2✔
5570
        Group& group = wt.get_group();
2✔
5571
        TableRef origin = group.get_table("origin");
2✔
5572
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5573
        for (i = 0; i < (REALM_MAX_BPNODE_SIZE - 1); i++) {
2,000✔
5574
            lv0.add(target_keys[i]);
1,998✔
5575
        }
1,998✔
5576
        wt.commit();
2✔
5577
    }
2✔
5578

1✔
5579
    auto reader = sg->start_read();
2✔
5580
    auto lv1 = reader->get_table("origin")->begin()->get_linklist(col);
2✔
5581
    {
2✔
5582
        WriteTransaction wt(sg);
2✔
5583
        Group& group = wt.get_group();
2✔
5584
        TableRef origin = group.get_table("origin");
2✔
5585
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5586
        lv0.add(target_keys[i++]);
2✔
5587
        lv0.add(target_keys[i++]);
2✔
5588
        wt.commit();
2✔
5589
    }
2✔
5590

1✔
5591
    // If MAX_BPNODE_SIZE is 4 and we run in debug mode, then the LinkView
1✔
5592
    // accessor was not refreshed correctly. It would still be a leaf class,
1✔
5593
    // but the header flags would tell it is a node.
1✔
5594
    reader->advance_read();
2✔
5595
    CHECK_EQUAL(lv1.size(), i);
2✔
5596
}
2✔
5597

5598
TEST(LangBindHelper_Bug2295)
5599
{
2✔
5600
    SHARED_GROUP_TEST_PATH(path);
2✔
5601
    ShortCircuitHistory hist;
2✔
5602
    DBRef sg = DB::create(hist, path, DBOptions(crypt_key()));
2✔
5603
    int i;
2✔
5604
    std::vector<ObjKey> target_keys;
2✔
5605
    std::vector<ObjKey> origin_keys;
2✔
5606
    ColKey col;
2✔
5607
    {
2✔
5608
        WriteTransaction wt(sg);
2✔
5609
        Group& group = wt.get_group();
2✔
5610
        TableRef target = group.add_table("target");
2✔
5611
        target->add_column(type_Int, "data");
2✔
5612
        target->create_objects(REALM_MAX_BPNODE_SIZE + 2, target_keys);
2✔
5613
        TableRef origin = group.add_table("origin");
2✔
5614
        col = origin->add_column_list(*target, "_link");
2✔
5615
        origin->create_objects(2, origin_keys);
2✔
5616
        wt.commit();
2✔
5617
    }
2✔
5618

1✔
5619
    {
2✔
5620
        WriteTransaction wt(sg);
2✔
5621
        Group& group = wt.get_group();
2✔
5622
        TableRef origin = group.get_table("origin");
2✔
5623
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5624
        for (i = 0; i < (REALM_MAX_BPNODE_SIZE - 1); i++) {
2,000✔
5625
            lv0.add(target_keys[i]);
1,998✔
5626
        }
1,998✔
5627
        wt.commit();
2✔
5628
    }
2✔
5629

1✔
5630
    auto reader = sg->start_read();
2✔
5631
    auto lv1 = reader->get_table("origin")->begin()->get_linklist(col);
2✔
5632
    CHECK_EQUAL(lv1.size(), i);
2✔
5633
    {
2✔
5634
        WriteTransaction wt(sg);
2✔
5635
        Group& group = wt.get_group();
2✔
5636
        TableRef origin = group.get_table("origin");
2✔
5637
        // With the error present, this will cause some areas to be freed
1✔
5638
        // that has already been freed in the above transaction
1✔
5639
        auto lv0 = origin->begin()->get_linklist(col);
2✔
5640
        lv0.add(target_keys[i++]);
2✔
5641
        wt.commit();
2✔
5642
    }
2✔
5643
    reader->promote_to_write();
2✔
5644
    // Here we write the duplicates to the free list
1✔
5645
    reader->commit_and_continue_as_read();
2✔
5646
    reader->verify();
2✔
5647
    CHECK_EQUAL(lv1.size(), i);
2✔
5648
}
2✔
5649

5650
#ifdef LEGACY_TESTS // FIXME: Requires get_at() method to be available on Obj.
5651
ONLY(LangBindHelper_BigBinary)
5652
{
5653
    SHARED_GROUP_TEST_PATH(path);
5654
    ShortCircuitHistory hist;
5655
    DBRef sg = DB::create(hist, path);
5656
    std::string big_data(0x1000000, 'x');
5657
    auto rt = sg->start_read();
5658
    auto wt = sg->start_write();
5659

5660
    std::string data(16777362, 'y');
5661
    TableRef target = wt->add_table("big");
5662
    auto col = target->add_column(type_Binary, "data");
5663
    target->create_object().set(col, BinaryData(data.data(), data.size()));
5664
    wt->commit();
5665
    rt->advance_read();
5666
    {
5667
        WriteTransaction wt(sg);
5668
        TableRef t = wt.get_table("big");
5669
        t->begin()->set(col, BinaryData(big_data.data(), big_data.size()));
5670
        wt.get_group().verify();
5671
        wt.commit();
5672
    }
5673
    rt->advance_read();
5674
    auto t = rt->get_table("big");
5675
    size_t pos = 0;
5676
    BinaryData bin = t->begin()->get_at(col, pos); // <---- not there yet?
5677
    CHECK_EQUAL(memcmp(big_data.data(), bin.data(), bin.size()), 0);
5678
}
5679
#endif
5680

5681
TEST(LangBindHelper_CopyOnWriteOverflow)
5682
{
2✔
5683
    SHARED_GROUP_TEST_PATH(path);
2✔
5684
    ShortCircuitHistory hist;
2✔
5685
    DBRef sg = DB::create(hist, path);
2✔
5686
    auto g = sg->start_write();
2✔
5687
    auto table = g->add_table("big");
2✔
5688
    auto obj = table->create_object();
2✔
5689
    auto col = table->add_column(type_Binary, "data");
2✔
5690
    std::string data(0xfffff0, 'x');
2✔
5691
    obj.set(col, BinaryData(data.data(), data.size()));
2✔
5692
    g->commit();
2✔
5693
    g = sg->start_write();
2✔
5694
    g->get_table("big")->begin()->set(col, BinaryData{"Hello", 5});
2✔
5695
    g->verify();
2✔
5696
    g->commit();
2✔
5697
}
2✔
5698

5699

5700
TEST(LangBindHelper_RollbackOptimize)
5701
{
2✔
5702
    SHARED_GROUP_TEST_PATH(path);
2✔
5703
    const char* key = crypt_key();
2✔
5704
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5705
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(key));
2✔
5706
    auto g = sg_w->start_write();
2✔
5707

1✔
5708
    auto table = g->add_table("t0");
2✔
5709
    auto col = table->add_column(type_String, "str_col_0", true);
2✔
5710
    g->commit_and_continue_as_read();
2✔
5711
    g->verify();
2✔
5712
    g->promote_to_write();
2✔
5713
    g->verify();
2✔
5714
    std::vector<ObjKey> keys;
2✔
5715
    table->create_objects(198, keys);
2✔
5716
    table->enumerate_string_column(col);
2✔
5717
    g->rollback_and_continue_as_read();
2✔
5718
    g->verify();
2✔
5719
}
2✔
5720

5721

5722
TEST(LangBindHelper_BinaryReallocOverMax)
5723
{
2✔
5724
    SHARED_GROUP_TEST_PATH(path);
2✔
5725
    const char* key = crypt_key();
2✔
5726
    std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5727
    DBRef sg_w = DB::create(*hist_w, path, DBOptions(key));
2✔
5728
    auto g = sg_w->start_write();
2✔
5729
    auto table = g->add_table("table");
2✔
5730
    auto col = table->add_column(type_Binary, "binary_col", false);
2✔
5731
    auto obj = table->create_object();
2✔
5732

1✔
5733
    // The sizes of these binaries were found with AFL. Essentially we must hit
1✔
5734
    // the case where doubling the allocated memory goes above max_array_payload
1✔
5735
    // and hits the condition to clamp to the maximum.
1✔
5736
    std::string blob1(8877637, static_cast<unsigned char>(133));
2✔
5737
    std::string blob2(15994373, static_cast<unsigned char>(133));
2✔
5738
    BinaryData dataAlloc(blob1);
2✔
5739
    BinaryData dataRealloc(blob2);
2✔
5740

1✔
5741
    obj.set(col, dataAlloc);
2✔
5742
    obj.set(col, dataRealloc);
2✔
5743
    g->verify();
2✔
5744
}
2✔
5745

5746

5747
// This test verifies that small unencrypted files are treated correctly if
5748
// opened as encrypted.
5749
#if REALM_ENABLE_ENCRYPTION
5750
TEST(LangBindHelper_OpenAsEncrypted)
5751
{
2✔
5752
    SHARED_GROUP_TEST_PATH(path);
2✔
5753
    {
2✔
5754
        ShortCircuitHistory hist;
2✔
5755
        DBRef sg_clear = DB::create(hist, path);
2✔
5756

1✔
5757
        {
2✔
5758
            WriteTransaction wt(sg_clear);
2✔
5759
            TableRef target = wt.add_table("table");
2✔
5760
            target->add_column(type_String, "mixed_col");
2✔
5761
            target->create_object();
2✔
5762
            wt.commit();
2✔
5763
        }
2✔
5764
    }
2✔
5765
    {
2✔
5766
        const char* key = crypt_key(true);
2✔
5767
        std::unique_ptr<Replication> hist_encrypt(make_in_realm_history());
2✔
5768
        CHECK_THROW(DB::create(*hist_encrypt, path, DBOptions(key)), InvalidDatabase);
2✔
5769
    }
2✔
5770
}
2✔
5771
#endif
5772

5773

5774
// Test case generated in [realm-core-4.0.4] on Mon Dec 18 13:33:24 2017.
5775
// Adding 0 rows to a StringEnumColumn would add the default value to the keys
5776
// but not the indexes creating an inconsistency.
5777
TEST(LangBindHelper_EnumColumnAddZeroRows)
5778
{
2✔
5779
    SHARED_GROUP_TEST_PATH(path);
2✔
5780
    const char* key = nullptr;
2✔
5781
    std::unique_ptr<Replication> hist(make_in_realm_history());
2✔
5782
    DBRef sg = DB::create(*hist, path, DBOptions(key));
2✔
5783
    auto g = sg->start_write();
2✔
5784
    auto g_r = sg->start_read();
2✔
5785
    auto table = g->add_table("");
2✔
5786

1✔
5787
    auto col = table->add_column(DataType(2), "table", false);
2✔
5788
    table->enumerate_string_column(col);
2✔
5789
    g->commit_and_continue_as_read();
2✔
5790
    g->verify();
2✔
5791
    g->promote_to_write();
2✔
5792
    g->verify();
2✔
5793
    table->create_object();
2✔
5794
    g->commit_and_continue_as_read();
2✔
5795
    g_r->advance_read();
2✔
5796
    g_r->verify();
2✔
5797
    g->verify();
2✔
5798
}
2✔
5799

5800

5801
TEST(LangBindHelper_RemoveObject)
5802
{
2✔
5803
    SHARED_GROUP_TEST_PATH(path);
2✔
5804
    ShortCircuitHistory hist;
2✔
5805
    DBRef sg = DB::create(hist, path);
2✔
5806
    ColKey col;
2✔
5807
    auto rt = sg->start_read();
2✔
5808
    {
2✔
5809
        auto wt = sg->start_write();
2✔
5810
        TableRef t = wt->add_table("Foo");
2✔
5811
        col = t->add_column(type_Int, "int");
2✔
5812
        t->create_object(ObjKey(123)).set(col, 1);
2✔
5813
        t->create_object(ObjKey(456)).set(col, 2);
2✔
5814
        wt->commit();
2✔
5815
    }
2✔
5816

1✔
5817
    rt->advance_read();
2✔
5818
    auto table = rt->get_table("Foo");
2✔
5819
    const Obj o1 = table->get_object(ObjKey(123));
2✔
5820
    const Obj o2 = table->get_object(ObjKey(456));
2✔
5821
    CHECK_EQUAL(o1.get<int64_t>(col), 1);
2✔
5822
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
5823

1✔
5824
    {
2✔
5825
        auto wt = sg->start_write();
2✔
5826
        TableRef t = wt->get_table("Foo");
2✔
5827
        t->remove_object(ObjKey(123));
2✔
5828
        wt->commit();
2✔
5829
    }
2✔
5830
    rt->advance_read();
2✔
5831
    CHECK_THROW(o1.get<int64_t>(col), KeyNotFound);
2✔
5832
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
5833
}
2✔
5834

5835
TEST(LangBindHelper_callWithLock)
5836
{
2✔
5837
    SHARED_GROUP_TEST_PATH(path);
2✔
5838
    auto callback = [&](const std::string& realm_path) {
4✔
5839
        CHECK(realm_path.compare(path) == 0);
4✔
5840
    };
4✔
5841

1✔
5842
    auto callback_not_called = [&](const std::string&) {
1✔
5843
        CHECK(false);
×
5844
    };
×
5845

1✔
5846
    // call_with_lock should run the callback if the lock file doesn't exist.
1✔
5847
    CHECK_NOT(File::exists(path.get_lock_path()));
2✔
5848
    CHECK(DB::call_with_lock(path, callback));
2✔
5849
    CHECK(File::exists(path.get_lock_path()));
2✔
5850

1✔
5851
    {
2✔
5852
        std::unique_ptr<Replication> hist_w(make_in_realm_history());
2✔
5853
        DBRef sg_w = DB::create(*hist_w, path);
2✔
5854
        WriteTransaction wt(sg_w);
2✔
5855
        CHECK_NOT(DB::call_with_lock(path, callback_not_called));
2✔
5856
        wt.commit();
2✔
5857
        CHECK_NOT(DB::call_with_lock(path, callback_not_called));
2✔
5858
    }
2✔
5859
    CHECK(DB::call_with_lock(path, callback));
2✔
5860
}
2✔
5861

5862
TEST(LangBindHelper_AdvanceReadCluster)
5863
{
2✔
5864
    SHARED_GROUP_TEST_PATH(path);
2✔
5865
    ShortCircuitHistory hist;
2✔
5866
    DBRef sg = DB::create(hist, path);
2✔
5867

1✔
5868
    auto rt = sg->start_read();
2✔
5869
    {
2✔
5870
        auto wt = sg->start_write();
2✔
5871
        TableRef t = wt->add_table("Foo");
2✔
5872
        auto int_col = t->add_column(type_Int, "int");
2✔
5873
        for (int64_t i = 0; i < 100; i++) {
202✔
5874
            t->create_object(ObjKey(i)).set(int_col, i);
200✔
5875
        }
200✔
5876
        wt->commit();
2✔
5877
    }
2✔
5878

1✔
5879
    rt->advance_read();
2✔
5880
    auto table = rt->get_table("Foo");
2✔
5881
    auto col = table->get_column_key("int");
2✔
5882
    for (int64_t i = 0; i < 100; i++) {
202✔
5883
        const Obj o = table->get_object(ObjKey(i));
200✔
5884
        CHECK_EQUAL(o.get<int64_t>(col), i);
200✔
5885
    }
200✔
5886
}
2✔
5887

5888
TEST(LangBindHelper_ImportDetachedLinkList)
5889
{
2✔
5890
    SHARED_GROUP_TEST_PATH(path);
2✔
5891
    auto hist = make_in_realm_history();
2✔
5892
    DBRef db = DB::create(*hist, path);
2✔
5893
    std::unique_ptr<TableView> tv_1;
2✔
5894

1✔
5895
    ColKey col_pet;
2✔
5896
    ColKey col_addr;
2✔
5897
    ColKey col_name;
2✔
5898
    ColKey col_age;
2✔
5899

1✔
5900
    {
2✔
5901
        WriteTransaction wt(db);
2✔
5902
        auto persons = wt.add_table("person");
2✔
5903
        auto dogs = wt.add_table("dog");
2✔
5904
        col_pet = persons->add_column_list(*dogs, "pet");
2✔
5905
        col_addr = persons->add_column_list(type_String, "address");
2✔
5906
        col_name = dogs->add_column(type_String, "name");
2✔
5907
        col_age = dogs->add_column(type_Int, "age");
2✔
5908

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

1✔
5912
        auto me = persons->create_object();
2✔
5913
        me.set_list_values<String>(col_addr, {"Paradisæblevej 5", "2500 Andeby"});
2✔
5914
        auto pets = me.get_linklist(col_pet);
2✔
5915
        pets.add(tago.get_key());
2✔
5916
        pets.add(hector.get_key());
2✔
5917
        wt.commit();
2✔
5918
    }
2✔
5919

1✔
5920
    auto rt = db->start_read();
2✔
5921
    auto persons = rt->get_table("person");
2✔
5922
    auto dogs = rt->get_table("dog");
2✔
5923
    Obj me = *persons->begin();
2✔
5924
    auto my_pets = me.get_linklist(col_pet);
2✔
5925
    Query q = dogs->where(my_pets).equal(col_age, 7);
2✔
5926
    auto tv = q.find_all();
2✔
5927
    CHECK_EQUAL(tv.size(), 1);
2✔
5928
    auto my_address = me.get_listbase_ptr(col_addr);
2✔
5929
    CHECK_EQUAL(my_address->size(), 2);
2✔
5930

1✔
5931
    {
2✔
5932
        // Delete the person.
1✔
5933
        WriteTransaction wt(db);
2✔
5934
        wt.get_table("person")->begin()->remove();
2✔
5935
        wt.commit();
2✔
5936
    }
2✔
5937

1✔
5938
    {
2✔
5939
        auto read_transaction = db->start_read();
2✔
5940

1✔
5941
        // The link_list that is embedded in the query imported here should be detached
1✔
5942
        auto local_tv = read_transaction->import_copy_of(tv, PayloadPolicy::Stay);
2✔
5943
        local_tv->sync_if_needed();
2✔
5944
        CHECK_EQUAL(local_tv->size(), 0);
2✔
5945

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

1✔
5949
        // The list imported here should be null
1✔
5950
        CHECK_NOT(read_transaction->import_copy_of(*my_address));
2✔
5951
    }
2✔
5952

1✔
5953
    CHECK_EQUAL(tv_1->size(), 0);
2✔
5954
}
2✔
5955

5956
TEST(LangBindHelper_SearchIndexAccessor)
5957
{
2✔
5958
    SHARED_GROUP_TEST_PATH(path);
2✔
5959
    auto hist = make_in_realm_history();
2✔
5960
    DBRef db = DB::create(*hist, path);
2✔
5961
    ColKey col_name;
2✔
5962

1✔
5963
    auto tr = db->start_write();
2✔
5964
    {
2✔
5965
        auto persons = tr->add_table("person");
2✔
5966
        col_name = persons->add_column(type_String, "name");
2✔
5967
        persons->add_search_index(col_name);
2✔
5968
        persons->create_object().set(col_name, "Per");
2✔
5969
    }
2✔
5970
    tr->commit_and_continue_as_read();
2✔
5971

1✔
5972
    tr->promote_to_write();
2✔
5973
    {
2✔
5974
        auto persons = tr->get_table("person");
2✔
5975
        persons->remove_column(col_name);
2✔
5976
        auto col_age = persons->add_column(type_Int, "age");
2✔
5977
        persons->add_search_index(col_age);
2✔
5978
        // Index referring to col_age is now at position 0
1✔
5979
        persons->create_object().set(col_age, 47);
2✔
5980
    }
2✔
5981
    // The index accessor must be refreshed with old ColKey (col_name)
1✔
5982
    tr->rollback_and_continue_as_read();
2✔
5983

1✔
5984
    tr->promote_to_write();
2✔
5985
    {
2✔
5986
        auto persons = tr->get_table("person");
2✔
5987
        // Index accssor uses its ColKey to find value in table
1✔
5988
        persons->create_object().set(col_name, "Poul");
2✔
5989
    }
2✔
5990
    tr->commit();
2✔
5991
}
2✔
5992

5993
TEST(LangBindHelper_ArrayXoverMapping)
5994
{
2✔
5995
    SHARED_GROUP_TEST_PATH(path);
2✔
5996
    auto hist = make_in_realm_history();
2✔
5997
    DBRef db = DB::create(*hist, path);
2✔
5998
    ColKey my_col;
2✔
5999
    {
2✔
6000
        auto tr = db->start_write();
2✔
6001
        auto tbl = tr->add_table("my_table");
2✔
6002
        my_col = tbl->add_column(type_String, "my_col");
2✔
6003
        std::string s(1'000'000, 'a');
2✔
6004
        for (auto i = 0; i < 100; ++i)
202✔
6005
            tbl->create_object().set_all(s);
200✔
6006
        tr->commit();
2✔
6007
    }
2✔
6008
    REALM_ASSERT(db->compact());
2✔
6009
    {
2✔
6010
        auto tr = db->start_read();
2✔
6011
        auto tbl = tr->get_table("my_table");
2✔
6012
        for (auto i = 0; i < 100; ++i) {
202✔
6013
            auto o = tbl->get_object(i);
200✔
6014
            StringData str = o.get<String>(my_col);
200✔
6015
            for (auto j = 0; j < 1'000'000; ++j)
200,000,200✔
6016
                REALM_ASSERT(str[j] == 'a');
200✔
6017
        }
200✔
6018
    }
2✔
6019
}
2✔
6020

6021
TEST(LangBindHelper_SchemaChangeNotification)
6022
{
2✔
6023
    SHARED_GROUP_TEST_PATH(path);
2✔
6024
    auto hist = make_in_realm_history();
2✔
6025
    DBRef db = DB::create(*hist, path);
2✔
6026

1✔
6027
    auto rt = db->start_read();
2✔
6028
    bool handler_called;
2✔
6029
    rt->set_schema_change_notification_handler([&handler_called]() {
4✔
6030
        handler_called = true;
4✔
6031
    });
4✔
6032
    CHECK(rt->has_schema_change_notification_handler());
2✔
6033

1✔
6034
    {
2✔
6035
        auto tr = db->start_write();
2✔
6036
        tr->add_table("my_table");
2✔
6037
        tr->commit();
2✔
6038
    }
2✔
6039
    handler_called = false;
2✔
6040
    rt->advance_read();
2✔
6041
    CHECK(handler_called);
2✔
6042

1✔
6043
    {
2✔
6044
        auto tr = db->start_write();
2✔
6045
        auto table = tr->get_table("my_table");
2✔
6046
        table->add_column(type_Int, "integer");
2✔
6047
        tr->commit();
2✔
6048
    }
2✔
6049
    handler_called = false;
2✔
6050
    rt->advance_read();
2✔
6051
    CHECK(handler_called);
2✔
6052
}
2✔
6053

6054
TEST(LangBindHelper_InMemoryDB)
6055
{
2✔
6056
    DBRef sg = DB::create(make_in_realm_history());
2✔
6057
    ColKey col;
2✔
6058
    auto rt = sg->start_read();
2✔
6059
    {
2✔
6060
        auto wt = sg->start_write();
2✔
6061
        TableRef t = wt->add_table("Foo");
2✔
6062
        col = t->add_column(type_Int, "int");
2✔
6063
        t->create_object(ObjKey(123)).set(col, 1);
2✔
6064
        t->create_object(ObjKey(456)).set(col, 2);
2✔
6065
        wt->commit();
2✔
6066
    }
2✔
6067

1✔
6068
    rt->advance_read();
2✔
6069
    auto table = rt->get_table("Foo");
2✔
6070
    const Obj o1 = table->get_object(ObjKey(123));
2✔
6071
    const Obj o2 = table->get_object(ObjKey(456));
2✔
6072
    CHECK_EQUAL(o1.get<int64_t>(col), 1);
2✔
6073
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
6074

1✔
6075
    {
2✔
6076
        auto wt = sg->start_write();
2✔
6077
        TableRef t = wt->get_table("Foo");
2✔
6078
        t->remove_object(ObjKey(123));
2✔
6079
        wt->commit();
2✔
6080
    }
2✔
6081
    rt->advance_read();
2✔
6082
    CHECK_THROW(o1.get<int64_t>(col), KeyNotFound);
2✔
6083
    CHECK_EQUAL(o2.get<int64_t>(col), 2);
2✔
6084
}
2✔
6085

6086
#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

© 2026 Coveralls, Inc