File: simple_queue_test.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (76 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <thread>  // NOLINT

#include "caffe2/utils/simple_queue.h"
#include <gtest/gtest.h>

namespace caffe2 {

static std::unique_ptr<SimpleQueue<int> > gQueue;

static void ConsumerFunction(int thread_idx) {
  // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
  int value;
  while (true) {
    if (!gQueue->Pop(&value)) return;
    VLOG(1) << "Emitting " << value << " from thread " << thread_idx;
  }
}

static void ProducerFunction(int thread_idx, int start, int count) {
  for (int i = 0; i < count; ++i) {
    VLOG(1) << "Pushing " << i + start << " from thread " << thread_idx;
    gQueue->Push(i + start);
  }
}


TEST(SimpleQueueTest, SingleProducerSingleConsumer) {
  // NOLINTNEXTLINE(modernize-make-unique)
  gQueue.reset(new SimpleQueue<int>());
  std::thread consumer(ConsumerFunction, 0);
  for (int i = 0; i < 10; ++i) {
    gQueue->Push(i);
  }
  gQueue->NoMoreJobs();
  consumer.join();
}

TEST(SimpleQueueTest, SingleProducerDoubleConsumer) {
  // NOLINTNEXTLINE(modernize-make-unique)
  gQueue.reset(new SimpleQueue<int>());
  std::thread consumer0(ConsumerFunction, 0);
  std::thread consumer1(ConsumerFunction, 1);
  for (int i = 0; i < 10; ++i) {
    gQueue->Push(i);
  }
  gQueue->NoMoreJobs();
  consumer0.join();
  consumer1.join();
}


TEST(SimpleQueueTest, DoubleProducerDoubleConsumer) {
  // NOLINTNEXTLINE(modernize-make-unique)
  gQueue.reset(new SimpleQueue<int>());
  std::thread producer0(ProducerFunction, 0, 0, 10);
  std::thread producer1(ProducerFunction, 0, 10, 10);
  std::thread consumer0(ConsumerFunction, 2);
  std::thread consumer1(ConsumerFunction, 3);
  producer0.join();
  producer1.join();
  gQueue->NoMoreJobs();
  consumer0.join();
  consumer1.join();
}

TEST(SimpleQueueDeathTest, CannotAddAfterQueueFinished) {
  // NOLINTNEXTLINE(modernize-make-unique)
  gQueue.reset(new SimpleQueue<int>());
  gQueue->Push(0);
  gQueue->NoMoreJobs();
  // NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
  ASSERT_THROW(gQueue->Push(0), EnforceNotMet);
}


}  // namespace caffe2