tests: Provide alarm replacement for Windows

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Victor Toso <victortoso@redhat.com>
This commit is contained in:
Frediano Ziglio 2018-06-24 21:28:02 +01:00
parent 50be664da2
commit 8031558ee3
7 changed files with 98 additions and 0 deletions

View File

@ -35,6 +35,8 @@ libtest_a_SOURCES = \
test-display-base.h \
test-glib-compat.c \
test-glib-compat.h \
win-alarm.c \
win-alarm.h \
$(NULL)
LDADD = \

View File

@ -11,6 +11,8 @@ test_lib_sources = [
'test-display-base.h',
'test-glib-compat.c',
'test-glib-compat.h',
'win-alarm.c',
'win-alarm.h',
]
test_libs = []

View File

@ -28,6 +28,7 @@
#include "red-client.h"
#include "cursor-channel.h"
#include "net-utils.h"
#include "win-alarm.h"
/*
* Declare a RedTestChannel to be used for the test

View File

@ -31,6 +31,7 @@
#include <spice/macros.h>
#include <common/log.h>
#include "basic-event-loop.h"
#include "win-alarm.h"
static SpiceCoreInterface *core = NULL;
static GMainLoop *loop = NULL;

View File

@ -33,6 +33,7 @@
#include "test-glib-compat.h"
#include "stream-channel.h"
#include "reds.h"
#include "win-alarm.h"
static SpiceCharDeviceInstance vmc_instance;

65
server/tests/win-alarm.c Normal file
View File

@ -0,0 +1,65 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2018 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <glib.h>
#ifdef _WIN32
#include <windows.h>
#include "win-alarm.h"
static HANDLE alarm_cond = NULL;
static DWORD WINAPI alarm_thread_proc(LPVOID arg)
{
unsigned int timeout = (uintptr_t) arg;
switch (WaitForSingleObject(alarm_cond, timeout * 1000)) {
case WAIT_OBJECT_0:
return 0;
}
abort();
return 0;
}
void alarm(unsigned int timeout)
{
static HANDLE thread = NULL;
// create an event to stop the alarm thread
if (alarm_cond == NULL) {
alarm_cond = CreateEvent(NULL, TRUE, FALSE, NULL);
g_assert(alarm_cond != NULL);
}
// stop old alarm
if (thread) {
SetEvent(alarm_cond);
g_assert(WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0);
CloseHandle(thread);
thread = NULL;
}
if (timeout) {
ResetEvent(alarm_cond);
// start alarm thread
thread = CreateThread(NULL, 0, alarm_thread_proc, (LPVOID) (uintptr_t) timeout, 0, NULL);
g_assert(thread);
}
}
#endif

26
server/tests/win-alarm.h Normal file
View File

@ -0,0 +1,26 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2018 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TEST_WIN_ALARM_H
#define TEST_WIN_ALARM_H
#ifdef _WIN32
void test_alarm(unsigned int timeout);
#define alarm test_alarm
#endif
#endif // TEST_WIN_ALARM_H