MCPcopy Create free account
hub / github.com/apple/foundationdb / writer

Function writer

flow/WriteOnlySet.actor.cpp:219–252  ·  view source on GitHub ↗

In a loop adds and removes a set of objects to the set

Source from the content-addressed store, hash-verified

217
218// In a loop adds and removes a set of objects to the set
219void writer(std::shared_ptr<TestSet> set, std::chrono::seconds runFor) {
220 auto start = Clock::now();
221 std::random_device rDev;
222 DeterministicRandom rnd(rDev());
223 while (true) {
224 unsigned inserts = 0, erases = 0;
225 if (Clock::now() - start > runFor) {
226 return;
227 }
228 std::vector<TestSet::Index> positions;
229 for (int i = 0; i < rnd.randomInt(1, 101); ++i) {
230 Reference<TestObject> o(new TestObject());
231 auto pos = set->insert(o);
232 if (pos == TestSet::npos) {
233 // could not insert -- ignore
234 break;
235 }
236 ++inserts;
237 ASSERT(pos < TestSet::capacity);
238 positions.push_back(pos);
239 }
240 rnd.randomShuffle(positions);
241 for (auto p : positions) {
242 if (!set->erase(p)) {
243 ++numLockedErase;
244 }
245 ++erases;
246 }
247 numInserts.fetch_add(inserts);
248 numErase.fetch_add(erases);
249 ASSERT(inserts == erases);
250 std::this_thread::sleep_for(std::chrono::milliseconds(1));
251 }
252}
253
254// This unit test creates 5 writer threads and one copier thread.
255TEST_CASE("/flow/WriteOnlySet") {

Callers 1

Calls 6

nowFunction · 0.85
randomShuffleMethod · 0.80
randomIntMethod · 0.45
insertMethod · 0.45
push_backMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected