In a loop adds and removes a set of objects to the set
| 217 | |
| 218 | // In a loop adds and removes a set of objects to the set |
| 219 | void 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. |
| 255 | TEST_CASE("/flow/WriteOnlySet") { |
no test coverage detected