Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
EventTest.cpp
[詳解]
1
5
6#include "pch.h"
7#include "Event.h"
8#include "Thread.h"
9#include "TString.h"
10
11using namespace Microsoft::VisualStudio::CppUnitTestFramework;
12
13namespace WindowsLibraryTest
14{
15 TEST_CLASS (EventTest)
16 {
17 public:
18 TEST_CLASS_INITIALIZE (ClassInitialize)
19 {
20 Logger::WriteMessage ("EventTest class initialize.\n");
21 }
22
23 TEST_CLASS_CLEANUP (ClassCleanup)
24 {
25 Logger::WriteMessage ("EventTest class cleanup.\n");
26 }
27
28 TEST_METHOD_INITIALIZE (MethodInitialize)
29 {
30 Logger::WriteMessage ("EventTest method initialize.\n");
31 }
32
33 TEST_METHOD_CLEANUP (MethodCleanup)
34 {
35 Logger::WriteMessage ("EventTest method cleanup.\n");
36 }
37
38 TEST_METHOD (AutoEventTest)
39 {
40 auto EventFunction = [](LPVOID lpvParam)->DWORD
41 {
42 BOOL ret;
43 alt::Event event;
44 alt::Thread* thread = (alt::Thread*)lpvParam;
45
46 ret = event.Create (_T ("AutoEventTest"), FALSE, TRUE); // 自動リセットイベント
47
48 for (int i = 0; i < 10; i++)
49 {
50 ret = event.Pulse ();
51 Assert::IsTrue (ret, _T ("Event::PulseEvent() failed.\n"));
52 Logger::WriteMessage (_T ("PulseFunction() Event pulsed.\n"));
53 Sleep (100);
54 }
55
56 return 0;
57 };
58
59 auto RecvFunction = [](LPVOID lpvParam)->DWORD
60 {
61 alt::Event event;
62 alt::Thread* thread = (alt::Thread*)lpvParam;
63
64 BOOL ret = event.Open (_T ("AutoEventTest"));
65
66 for (int i = 0; i < 10; i++)
67 {
68 ret = event.Wait (10000); // 10秒
69 if (ret == WAIT_ABANDONED_0 || ret == WAIT_FAILED || ret == WAIT_TIMEOUT) break;
70 if (ret == WAIT_OBJECT_0)
71 {
72 alt::TString message;
73 message.Format (_T ("Event::Wait() wait breaked. ThreadID=%d\n"),::GetThreadId(thread->GetHandle()));
74 Logger::WriteMessage (message.Ctr());
75 }
76 }
77
78 return 0;
79 };
80
81 alt::Thread EventThread;
82 alt::Thread RecvThread1;
83 alt::Thread RecvThread2;
84 alt::Thread RecvThread3;
85
86 Assert::IsTrue (EventThread.Create (EventFunction, &EventThread), _T ("Failed to create PulseThread.\n"));
87 Assert::IsTrue (RecvThread1.Create (RecvFunction, &RecvThread1), _T ("Failed to create RecvThread1.\n"));
88 Assert::IsTrue (RecvThread2.Create (RecvFunction, &RecvThread2), _T ("Failed to create RecvThread2.\n"));
89 Assert::IsTrue (RecvThread3.Create (RecvFunction, &RecvThread3), _T ("Failed to create RecvThread3.\n"));
90
91 EventThread.Wait ();
92 }
93
94 TEST_METHOD (ManualEventTest)
95 {
96 auto EventFunction = [](LPVOID lpvParam)->DWORD
97 {
98 BOOL ret;
99 alt::Event event;
100 alt::Thread* thread = (alt::Thread*)lpvParam;
101
102 ret = event.Create (_T ("ManualEventTest"), TRUE, FALSE); // 手動リセットイベント
103
104 for (int i = 0; i < 10; i++)
105 {
106 ret = event.Set ();
107 Logger::WriteMessage (_T ("Event::Set()\n"));
108 Assert::IsTrue (ret, _T ("Event::SetEvent() failed.\n"));
109
110 Sleep (100);
111
112 ret = event.Reset ();
113 Logger::WriteMessage (_T ("Event::Reset()\n"));
114 Assert::IsTrue (ret, _T ("Event::ResetEvent() failed.\n"));
115
116 Sleep (100);
117 }
118
119 return 0;
120 };
121
122 auto RecvFunction = [](LPVOID lpvParam)->DWORD
123 {
124 alt::Event event;
125 alt::Thread* thread = (alt::Thread*)lpvParam;
126
127 BOOL ret = event.Open (_T ("ManualEventTest"));
128
129 for (;;)
130 {
131 ret = event.Wait (1000);
132 if (ret == WAIT_ABANDONED_0 || ret == WAIT_FAILED) break;
133
134 if (ret == WAIT_OBJECT_0)
135 {
136 alt::TString message;
137 message.Format (_T ("Event::Wait() wait breaked. ThreadID=%d\n"),::GetThreadId(thread->GetHandle()));
138 Logger::WriteMessage (message.Ctr());
139 }
140 if (ret == WAIT_TIMEOUT)
141 {
142 alt::TString message;
143 message.Format (_T ("Event::Wait() wait timeout. ThreadID=%d\n"),::GetThreadId(thread->GetHandle()));
144 Logger::WriteMessage (message.Ctr());
145 }
146
147 Sleep (200);
148 }
149
150 return 0;
151 };
152
153 alt::Thread EventThread;
154 alt::Thread RecvThread1;
155 alt::Thread RecvThread2;
156 alt::Thread RecvThread3;
157
158 Assert::IsTrue (EventThread.Create (EventFunction, &EventThread), _T ("Failed to create EventThread.\n"));
159 Assert::IsTrue (RecvThread1.Create (RecvFunction, &RecvThread1), _T ("Failed to create RecvThread1.\n"));
160 Assert::IsTrue (RecvThread2.Create (RecvFunction, &RecvThread2), _T ("Failed to create RecvThread2.\n"));
161 Assert::IsTrue (RecvThread3.Create (RecvFunction, &RecvThread3), _T ("Failed to create RecvThread3.\n"));
162
163 EventThread.Wait ();
164 }
165 };
166}
イベントに関するWindowsAPIを集約したクラス
文字列に関するWindowsAPIを集約したクラス
スレッドに関するWindowsAPIを集約したクラス
プリコンパイル済みヘッダー ファイルです。
イベントに関するWindowsAPIを集約したクラス
Definition: Event.h:16
HANDLE APIENTRY GetHandle() const
継承先はこの関数でハンドルを取得します。
文字列に関するWindowsAPIを集約したクラス
Definition: TString.h:17
TString &APIENTRY Format(LPCTSTR format,...)
フォーマットに従ってパラメータを文字列化します。
Definition: TString.cpp:333
LPCTSTR APIENTRY Ctr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:46
スレッドに関するWindowsAPIを集約したクラス
Definition: Thread.h:43
BOOL APIENTRY Create(PTHREAD_START_ROUTINE function=NULL, LPVOID lpvParam=NULL, DWORD dwCreationFlag=0)
スレッドを作成します。
Definition: Thread.cpp:24
DWORD APIENTRY Wait(DWORD dwWaitTime=INFINITE) const
シグナル状態になるとブロックを解除します。
TEST_CLASS(ArrayTest)
Definition: ArrayTest.cpp:19