summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForMultipleObjects/test1/test1.cpp
blob: 8249c38d9d1c5e6a36952b11a8394c09d437faf7 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
** Source: test1.c 
**
** Purpose: Test for WaitForMultipleObjects. Call the function
** on an array of 4 events, and ensure that it returns correct
** results when we do so.
**
**
**=========================================================*/

#include <palsuite.h>

/* Number of events in array */
#define MAX_EVENTS      4

BOOL WaitForMultipleObjectsTest()
{
    BOOL bRet = TRUE;
    DWORD dwRet = 0;

    DWORD i = 0, j = 0;

    LPSECURITY_ATTRIBUTES lpEventAttributes = NULL;
    BOOL bManualReset = TRUE; 
    BOOL bInitialState = TRUE;

    HANDLE hEvent[MAX_EVENTS];

    /* Run through this for loop and create 4 events */ 
    for (i = 0; i < MAX_EVENTS; i++)
    {
        hEvent[i] = CreateEvent( lpEventAttributes, 
                                 bManualReset, bInitialState, NULL);  

        if (hEvent[i] == INVALID_HANDLE_VALUE)
        {
            Trace("WaitForMultipleObjectsTest:CreateEvent %u failed (%x)\n", i, GetLastError());
            bRet = FALSE;
            break;
        }
        
        /* Set the current event */
        bRet = SetEvent(hEvent[i]);

        if (!bRet)
        {
            Trace("WaitForMultipleObjectsTest:SetEvent %u failed (%x)\n", i, GetLastError());
            bRet = FALSE;
            break;
        }
        
        /* Ensure that this returns the correct value */
        dwRet = WaitForSingleObject(hEvent[i],0);

        if (dwRet != WAIT_OBJECT_0)
        {
            Trace("WaitForMultipleObjectsTest:WaitForSingleObject %u failed (%x)\n", i, GetLastError());
            bRet = FALSE;
            break;
        }

        /* Reset the event, and again ensure that the return value of
           WaitForSingle is correct.
        */
        bRet = ResetEvent(hEvent[i]);

        if (!bRet)
        {
            Trace("WaitForMultipleObjectsTest:ResetEvent %u failed (%x)\n", i, GetLastError());
            bRet = FALSE;
            break;
        }
        
        dwRet = WaitForSingleObject(hEvent[i],0);

        if (dwRet != WAIT_TIMEOUT)
        {
            Trace("WaitForMultipleObjectsTest:WaitForSingleObject %u failed (%x)\n", i, GetLastError());
            bRet = FALSE;
            break;
        }
    }
    
    /* 
     * If the first section of the test passed, move on to the
     * second. 
    */

    if (bRet)
    {
        BOOL bWaitAll = TRUE;
        DWORD nCount = MAX_EVENTS;
        CONST HANDLE *lpHandles = &hEvent[0]; 

        /* Call WaitForMultipleOjbects on all the events, the return
           should be WAIT_TIMEOUT
        */
        dwRet = WaitForMultipleObjects( nCount, 
                                        lpHandles, 
                                        bWaitAll, 
                                        0);

        if (dwRet != WAIT_TIMEOUT)
        {
            Trace("WaitForMultipleObjectsTest:WaitForMultipleObjects failed (%x)\n", GetLastError());
        }
        else
        {
            /* Step through each event and one at a time, set the
               currect test, while reseting all the other tests
            */
            
            for (i = 0; i < MAX_EVENTS; i++)
            {
                for (j = 0; j < MAX_EVENTS; j++)
                {
                    if (j == i)
                    {
                        
                        bRet = SetEvent(hEvent[j]);
                        
                        if (!bRet)
                        {
                            Trace("WaitForMultipleObjectsTest:SetEvent %u failed (%x)\n", j, GetLastError());
                            break;
                        }
                    }
                    else
                    {
                        bRet = ResetEvent(hEvent[j]);
                        
                        if (!bRet)
                        {
                            Trace("WaitForMultipleObjectsTest:ResetEvent %u failed (%x)\n", j, GetLastError());
                        }
                    }
                }
                
                bWaitAll = FALSE;

                /* Check that WaitFor returns WAIT_OBJECT + i */ 
                dwRet = WaitForMultipleObjects( nCount, 
                                                lpHandles, bWaitAll, 0);
                
                if (dwRet != WAIT_OBJECT_0+i)
                {
                    Trace("WaitForMultipleObjectsTest:WaitForMultipleObjects failed (%x)\n", GetLastError());
                    bRet = FALSE;
                    break;
                }
            }
        }
        
        for (i = 0; i < MAX_EVENTS; i++)
        {
            bRet = CloseHandle(hEvent[i]);
            
            if (!bRet)
            {
                Trace("WaitForMultipleObjectsTest:CloseHandle %u failed (%x)\n", i, GetLastError());
            }
        }
    }
    
    return bRet;
}

BOOL WaitMultipleDuplicateHandleTest()
{
    BOOL testResult = TRUE;
    const HANDLE eventHandle = CreateEvent(NULL, TRUE, TRUE, NULL);
    HANDLE eventHandles[] = {eventHandle, eventHandle};

    // WaitAny - Wait for any of the events (no error expected)
    DWORD result = WaitForMultipleObjects(sizeof(eventHandles) / sizeof(eventHandles[0]), eventHandles, FALSE, 0);
    if (result != WAIT_OBJECT_0)
    {
        Trace("WaitMultipleDuplicateHandleTest:WaitAny failed (%x)\n", GetLastError());
        testResult = FALSE;
    }

    // WaitAll - Wait for all of the events (error expected)
    result = WaitForMultipleObjects(sizeof(eventHandles) / sizeof(eventHandles[0]), eventHandles, TRUE, 0);
    if (result != WAIT_FAILED)
    {
        Trace("WaitMultipleDuplicateHandleTest:WaitAll failed: call unexpectedly succeeded\n");
        testResult = FALSE;
    }
    else if (GetLastError() != ERROR_INVALID_PARAMETER)
    {
        Trace("WaitMultipleDuplicateHandleTest:WaitAll failed: unexpected last error (%x)\n");
        testResult = FALSE;
    }

    return testResult;
}

int __cdecl main(int argc, char **argv)
{
    
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return ( FAIL );
    }
    
    if(!WaitForMultipleObjectsTest())
    {
        Fail ("Test failed\n");
    }

    if (!WaitMultipleDuplicateHandleTest())
    {
        Fail("Test failed\n");
    }

    PAL_Terminate();
    return ( PASS );

}