async-win32-test.c++ 4.71 KB
Newer Older
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
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if _WIN32

#include "async-win32.h"
#include "thread.h"
#include "test.h"

namespace kj {
namespace {

KJ_TEST("Win32IocpEventPort I/O operations") {
  Win32IocpEventPort port;
  EventLoop loop(port);
  WaitScope waitScope(loop);

  auto pipeName = kj::str("\\\\.\\Pipe\\kj-async-win32-test.", GetCurrentProcessId());

  HANDLE readEnd_, writeEnd_;
  KJ_WIN32(readEnd_ = CreateNamedPipeA(pipeName.cStr(),
      PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
      PIPE_TYPE_BYTE | PIPE_WAIT,
      1, 0, 0, 0, NULL));
  AutoCloseHandle readEnd(readEnd_);

  KJ_WIN32(writeEnd_ = CreateFileA(pipeName.cStr(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
                                   FILE_ATTRIBUTE_NORMAL, NULL));
  AutoCloseHandle writeEnd(writeEnd_);

  auto observer = port.observeIo(readEnd);
  auto op = observer->newOperation(0);

  byte buffer[256];

  KJ_ASSERT(!ReadFile(readEnd, buffer, sizeof(buffer), NULL, op->getOverlapped()));
  DWORD error = GetLastError();
  if (error != ERROR_IO_PENDING) {
    KJ_FAIL_WIN32("ReadFile()", error);
  }

  bool done = false;
  auto promise = op->onComplete().then([&](Win32EventPort::IoResult result) {
    done = true;
    return result;
  }).eagerlyEvaluate(nullptr);

  KJ_EXPECT(!done);

  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);

  KJ_EXPECT(!done);

  DWORD bytesWritten;
  KJ_WIN32(WriteFile(writeEnd, "foo", 3, &bytesWritten, NULL));
  KJ_EXPECT(bytesWritten == 3);

  auto result = promise.wait(waitScope);
  KJ_EXPECT(result.errorCode == ERROR_SUCCESS);
  KJ_EXPECT(result.bytesTransferred == 3);

  KJ_EXPECT(kj::str(kj::arrayPtr(buffer, 3).asChars()) == "foo");
}

KJ_TEST("Win32IocpEventPort::wake()") {
  Win32IocpEventPort port;

  Thread thread([&]() {
    Sleep(10);
    port.wake();
  });

  KJ_EXPECT(port.wait());
}

KJ_TEST("Win32IocpEventPort::wake() on poll()") {
  Win32IocpEventPort port;
  volatile bool woken = false;

  Thread thread([&]() {
    Sleep(10);
    port.wake();
    woken = true;
  });

  KJ_EXPECT(!port.poll());
  while (!woken) Sleep(10);
  KJ_EXPECT(port.poll());
}

KJ_TEST("Win32IocpEventPort timer") {
  Win32IocpEventPort port;
  EventLoop loop(port);
  WaitScope waitScope(loop);

  auto start = port.getTimer().now();

  bool done = false;
  auto promise = port.getTimer().afterDelay(10 * MILLISECONDS).then([&]() {
    done = true;
  }).eagerlyEvaluate(nullptr);

  KJ_EXPECT(!done);

  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);
  evalLater([]() {}).wait(waitScope);

  KJ_EXPECT(!done);

  promise.wait(waitScope);
  KJ_EXPECT(done);
  KJ_EXPECT(port.getTimer().now() - start >= 10 * MILLISECONDS);
}

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
VOID CALLBACK testApcProc(ULONG_PTR dwParam) {
  reinterpret_cast<kj::PromiseFulfiller<void>*>(dwParam)->fulfill();
}

KJ_TEST("Win32IocpEventPort APC") {
  if (GetProcAddress(GetModuleHandle("ntdll.dll"), "wine_get_version") != nullptr) {
    // TODO(cleanup): Periodically check if Wine supports this yet.
    KJ_LOG(WARNING, "detected that we're running under wine and this test won't work; skipping");
    return;
  }

  Win32IocpEventPort port;
  EventLoop loop(port);
  WaitScope waitScope(loop);

  port.allowApc();

  auto paf = kj::newPromiseAndFulfiller<void>();

  KJ_WIN32(QueueUserAPC(&testApcProc, GetCurrentThread(),
      reinterpret_cast<ULONG_PTR>(paf.fulfiller.get())));

  paf.promise.wait(waitScope);
}

165 166 167 168
}  // namespace
}  // namespace kj

#endif  // _WIN32