From: Sebastian Lackner Subject: [1/2] ntdll/tests: Add tests for TpSimpleTryPost function. Message-Id: <54EC011B.6030102@fds-team.de> Date: Tue, 24 Feb 2015 05:42:03 +0100 --- dlls/ntdll/tests/Makefile.in | 1 + dlls/ntdll/tests/threadpool.c | 129 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 dlls/ntdll/tests/threadpool.c From 466fe3e2f258b0f9de4af37f6246fe9c2b92f579 Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Sun, 1 Feb 2015 18:14:09 +0100 Subject: ntdll/tests: Add tests for TpSimpleTryPost function. --- dlls/ntdll/tests/Makefile.in | 1 + dlls/ntdll/tests/threadpool.c | 129 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 dlls/ntdll/tests/threadpool.c diff --git a/dlls/ntdll/tests/Makefile.in b/dlls/ntdll/tests/Makefile.in index 81b4466..fc352dd 100644 --- a/dlls/ntdll/tests/Makefile.in +++ b/dlls/ntdll/tests/Makefile.in @@ -21,4 +21,5 @@ C_SRCS = \ rtlbitmap.c \ rtlstr.c \ string.c \ + threadpool.c \ time.c diff --git a/dlls/ntdll/tests/threadpool.c b/dlls/ntdll/tests/threadpool.c new file mode 100644 index 0000000..7b91779 --- /dev/null +++ b/dlls/ntdll/tests/threadpool.c @@ -0,0 +1,129 @@ +/* + * Unit test suite for thread pool functions + * + * Copyright 2015 Sebastian Lackner + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "ntdll_test.h" + +static HMODULE hntdll = 0; +static NTSTATUS (WINAPI *pTpAllocPool)(TP_POOL **,PVOID); +static VOID (WINAPI *pTpReleasePool)(TP_POOL *); +static NTSTATUS (WINAPI *pTpSimpleTryPost)(PTP_SIMPLE_CALLBACK,PVOID,TP_CALLBACK_ENVIRON *); + +#define NTDLL_GET_PROC(func) \ + do \ + { \ + p ## func = (void *)GetProcAddress(hntdll, #func); \ + if (!p ## func) trace("Failed to get address for %s\n", #func); \ + } \ + while (0) + +static BOOL init_threadpool(void) +{ + hntdll = GetModuleHandleA("ntdll"); + if (!hntdll) + { + win_skip("Could not load ntdll\n"); + return FALSE; + } + + NTDLL_GET_PROC(TpAllocPool); + NTDLL_GET_PROC(TpReleasePool); + NTDLL_GET_PROC(TpSimpleTryPost); + + if (!pTpAllocPool) + { + skip("Threadpool functions not supported, skipping tests\n"); + return FALSE; + } + + return TRUE; +} + +#undef NTDLL_GET_PROC + + +static void CALLBACK simple_cb(TP_CALLBACK_INSTANCE *instance, void *userdata) +{ + trace("Running simple callback\n"); + Sleep(100); + InterlockedIncrement((LONG *)userdata); +} + +static void test_tp_simple(void) +{ + TP_CALLBACK_ENVIRON environment; + TP_POOL *pool; + NTSTATUS status; + LONG userdata; + + /* post the callback using the default threadpool */ + userdata = 0; + memset(&environment, 0, sizeof(environment)); + environment.Version = 1; + environment.Pool = NULL; + status = pTpSimpleTryPost(simple_cb, &userdata, &environment); + ok(!status, "TpSimpleTryPost failed with status %x\n", status); + while (userdata == 0) Sleep(10); + ok(userdata == 1, "expected userdata = 1, got %u\n", userdata); + + /* allocate new threadpool */ + pool = NULL; + status = pTpAllocPool(&pool, NULL); + ok(!status, "TpAllocPool failed with status %x\n", status); + ok(pool != NULL, "expected pool != NULL\n"); + + /* post the callback using new threadpool */ + userdata = 0; + memset(&environment, 0, sizeof(environment)); + environment.Version = 1; + environment.Pool = pool; + status = pTpSimpleTryPost(simple_cb, &userdata, &environment); + ok(!status, "TpSimpleTryPost failed with status %x\n", status); + while (userdata == 0) Sleep(10); + ok(userdata == 1, "expected userdata = 1, got %u\n", userdata); + + /* test with invalid version number */ + userdata = 0; + memset(&environment, 0, sizeof(environment)); + environment.Version = 9999; + environment.Pool = pool; + status = pTpSimpleTryPost(simple_cb, &userdata, &environment); + todo_wine + ok(status == STATUS_INVALID_PARAMETER || broken(!status) /* Vista/2008 */, + "TpSimpleTryPost unexpectedly returned status %x\n", status); + if (!status) + { + while (userdata == 0) Sleep(10); + ok(userdata == 1, "expected userdata = 1, got %u\n", userdata); + } + + /* cleanup */ + pTpReleasePool(pool); +} + +START_TEST(threadpool) +{ + if (!init_threadpool()) + return; + + test_tp_simple(); + + /* make sure worker threads have terminated */ + Sleep(100); +} -- 2.3.0