Files
sdk/runtime/vm/port_test.cc
T
turnidge@google.com 9d72035ce5 Use the ThreadPool for all isolates and native ports. Previously,
each isolate or native port had a dedicated thread.

Refactored the MessageHandler api...

- Added a Run function to allow a MessageHandler to run on a
  ThreadPool.  These functions take a start and end callback to allow for
  isolate initialization and shutdown.

- Made the queue private to the MessageHandler and moved all message
  processing code inside the MessageHandler (got rid of all of the
  different flavors of RunLoop).  This helps remove some code
  duplication and hides the details of how messages are handled.

- Moved all locking and notification out of MessageQueue and moved it
  up to MessageHandler.  Moved OOB support out of MessageQueue and up
  to MessageHandler.  These changes make the MessageQueue much
  simpler.

- Refactored native port and isolate MessageHandlers to share more code.

- Improved --trace_isolates output.

- Added tests for MessageHandler.

Refactored lib/isolate code...

- Use the new MessageHandler::Run api.

- Got rid of the LongJump stuff in RunIsolate.  No longer needed.

- Use the new StartIsolateScope/SwitchIsolateScope to make the code
  less verbose and less error-prone.

- Store top-level isolate errors in the sticky_error.

Added StartIsolateScope/SwitchIsolateScope classes.
Review URL: https://chromiumcodereview.appspot.com//9924015

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6762 260f80e4-7a28-3924-810f-c04153c831b5
2012-04-19 19:47:27 +00:00

141 lines
3.7 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/assert.h"
#include "vm/message_handler.h"
#include "vm/os.h"
#include "vm/port.h"
#include "vm/unit_test.h"
namespace dart {
// Provides private access to PortMap for testing.
class PortMapTestPeer {
public:
static bool IsActivePort(Dart_Port port) {
MutexLocker ml(PortMap::mutex_);
return (PortMap::FindPort(port) >= 0);
}
static bool IsLivePort(Dart_Port port) {
MutexLocker ml(PortMap::mutex_);
intptr_t index = PortMap::FindPort(port);
if (index < 0) {
return false;
}
return PortMap::map_[index].live;
}
};
class PortTestMessageHandler : public MessageHandler {
public:
PortTestMessageHandler() : notify_count(0) {}
void MessageNotify(Message::Priority priority) {
notify_count++;
}
bool HandleMessage(Message* message) { return true; }
int notify_count;
};
TEST_CASE(PortMap_CreateAndCloseOnePort) {
PortTestMessageHandler handler;
intptr_t port = PortMap::CreatePort(&handler);
EXPECT_NE(0, port);
EXPECT(PortMapTestPeer::IsActivePort(port));
PortMap::ClosePort(port);
EXPECT(!PortMapTestPeer::IsActivePort(port));
}
TEST_CASE(PortMap_CreateAndCloseTwoPorts) {
PortTestMessageHandler handler;
Dart_Port port1 = PortMap::CreatePort(&handler);
Dart_Port port2 = PortMap::CreatePort(&handler);
EXPECT(PortMapTestPeer::IsActivePort(port1));
EXPECT(PortMapTestPeer::IsActivePort(port2));
// Uniqueness.
EXPECT_NE(port1, port2);
PortMap::ClosePort(port1);
EXPECT(!PortMapTestPeer::IsActivePort(port1));
EXPECT(PortMapTestPeer::IsActivePort(port2));
PortMap::ClosePort(port2);
EXPECT(!PortMapTestPeer::IsActivePort(port1));
EXPECT(!PortMapTestPeer::IsActivePort(port2));
}
TEST_CASE(PortMap_ClosePorts) {
PortTestMessageHandler handler;
Dart_Port port1 = PortMap::CreatePort(&handler);
Dart_Port port2 = PortMap::CreatePort(&handler);
EXPECT(PortMapTestPeer::IsActivePort(port1));
EXPECT(PortMapTestPeer::IsActivePort(port2));
// Close all ports at once.
PortMap::ClosePorts(&handler);
EXPECT(!PortMapTestPeer::IsActivePort(port1));
EXPECT(!PortMapTestPeer::IsActivePort(port2));
}
TEST_CASE(PortMap_CreateManyPorts) {
PortTestMessageHandler handler;
for (int i = 0; i < 32; i++) {
Dart_Port port = PortMap::CreatePort(&handler);
EXPECT(PortMapTestPeer::IsActivePort(port));
PortMap::ClosePort(port);
EXPECT(!PortMapTestPeer::IsActivePort(port));
}
}
TEST_CASE(PortMap_SetLive) {
PortTestMessageHandler handler;
intptr_t port = PortMap::CreatePort(&handler);
EXPECT_NE(0, port);
EXPECT(PortMapTestPeer::IsActivePort(port));
EXPECT(!PortMapTestPeer::IsLivePort(port));
PortMap::SetLive(port);
EXPECT(PortMapTestPeer::IsActivePort(port));
EXPECT(PortMapTestPeer::IsLivePort(port));
PortMap::ClosePort(port);
EXPECT(!PortMapTestPeer::IsActivePort(port));
EXPECT(!PortMapTestPeer::IsLivePort(port));
}
TEST_CASE(PortMap_PostMessage) {
PortTestMessageHandler handler;
Dart_Port port = PortMap::CreatePort(&handler);
EXPECT_EQ(0, handler.notify_count);
EXPECT(PortMap::PostMessage(new Message(
port, 0, reinterpret_cast<uint8_t*>(strdup("msg")),
Message::kNormalPriority)));
// Check that the message notify callback was called.
EXPECT_EQ(1, handler.notify_count);
PortMap::ClosePorts(&handler);
}
TEST_CASE(PortMap_PostMessageInvalidPort) {
EXPECT(!PortMap::PostMessage(new Message(
0, 0, reinterpret_cast<uint8_t*>(strdup("msg")),
Message::kNormalPriority)));
}
} // namespace dart