aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/attribute/exclusive_attribute_read_accessor/exclusive_attribute_read_accessor_test.cpp
blob: 7cb6a503ae8fb1ece43d173107596a0c8da4a441 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/searchcore/proton/attribute/exclusive_attribute_read_accessor.h>
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/common/sequencedtaskexecutor.h>
#include <vespa/vespalib/util/sync.h>

using namespace proton;
using namespace search;
using namespace vespalib;

using ReadGuard = ExclusiveAttributeReadAccessor::Guard;

AttributeVector::SP
createAttribute()
{
    attribute::Config cfg(attribute::BasicType::INT32, attribute::CollectionType::SINGLE);
    return search::AttributeFactory::createAttribute("myattr", cfg);
}

struct Fixture
{
    AttributeVector::SP attribute;
    SequencedTaskExecutor writer;
    ExclusiveAttributeReadAccessor accessor;

    Fixture()
        : attribute(createAttribute()),
          writer(1),
          accessor(attribute, writer)
    {}
};

TEST_F("require that attribute write thread is blocked while guard is held", Fixture)
{
    ReadGuard::UP guard = f.accessor.takeGuard();
    Gate gate;
    f.writer.execute("myattr", [&gate]() { gate.countDown(); });
    bool reachedZero = gate.await(100);
    EXPECT_FALSE(reachedZero);
    EXPECT_EQUAL(1u, gate.getCount());

    guard.reset();
    gate.await();
    EXPECT_EQUAL(0u, gate.getCount());
}

TEST_MAIN()
{
    TEST_RUN_ALL();
}