summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/synch/test/TestSynchEvent.c
blob: 083282ca4b040374219da4be4661881132f4b9e3 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <winpr/crt.h>
#include <winpr/synch.h>

int TestSynchEvent(int argc, char* argv[])
{
	HANDLE event = NULL;
	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);
	if (ResetEvent(NULL))
	{
		printf("ResetEvent(NULL) unexpectedly succeeded\n");
		return -1;
	}

	if (SetEvent(NULL))
	{
		printf("SetEvent(NULL) unexpectedly succeeded\n");
		return -1;
	}

	event = CreateEvent(NULL, TRUE, TRUE, NULL);

	if (!event)
	{
		printf("CreateEvent failure\n");
		return -1;
	}

	if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0)
	{
		printf("WaitForSingleObject failure 1\n");
		return -1;
	}

	if (!ResetEvent(event))
	{
		printf("ResetEvent failure with signaled event object\n");
		return -1;
	}

	if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
	{
		printf("WaitForSingleObject failure 2\n");
		return -1;
	}

	if (!ResetEvent(event))
	{
		/* Note: ResetEvent must also succeed if event is currently nonsignaled */
		printf("ResetEvent failure with nonsignaled event object\n");
		return -1;
	}

	if (!SetEvent(event))
	{
		printf("SetEvent failure with nonsignaled event object\n");
		return -1;
	}

	if (WaitForSingleObject(event, 0) != WAIT_OBJECT_0)
	{
		printf("WaitForSingleObject failure 3\n");
		return -1;
	}

	for (int i = 0; i < 10000; i++)
	{
		if (!SetEvent(event))
		{
			printf("SetEvent failure with signaled event object (i = %d)\n", i);
			return -1;
		}
	}

	if (!ResetEvent(event))
	{
		printf("ResetEvent failure after multiple SetEvent calls\n");
		return -1;
	}

	/* Independent of the amount of the previous SetEvent calls, a single
	   ResetEvent must be sufficient to get into nonsignaled state */

	if (WaitForSingleObject(event, 0) != WAIT_TIMEOUT)
	{
		printf("WaitForSingleObject failure 4\n");
		return -1;
	}

	CloseHandle(event);

	return 0;
}