1// Copyright 2017 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <lib/async/default.h> 6 7#include <threads.h> 8 9#include <lib/async-testutils/dispatcher_stub.h> 10#include <unittest/unittest.h> 11 12namespace { 13 14int default_test_thread(void*) { 15 BEGIN_TEST; 16 17 EXPECT_NULL(async_get_default_dispatcher(), "other thread's default is initially null"); 18 19 async::DispatcherStub async; 20 async_set_default_dispatcher(&async); 21 EXPECT_EQ(&async, async_get_default_dispatcher(), "other thread's default can be changed"); 22 23 END_TEST; 24 return 1; 25} 26 27bool get_set_default_test() { 28 BEGIN_TEST; 29 30 // Default is initially null. 31 EXPECT_NULL(async_get_default_dispatcher(), "default is initially null"); 32 33 // Default can be changed. 34 async::DispatcherStub async; 35 async_set_default_dispatcher(&async); 36 EXPECT_EQ(&async, async_get_default_dispatcher(), "default can be changed"); 37 38 // Default is thread-local. 39 thrd_t thread; 40 ASSERT_EQ(thrd_success, thrd_create(&thread, default_test_thread, nullptr), "thrd_create"); 41 int result; 42 ASSERT_EQ(thrd_success, thrd_join(thread, &result), "thrd_join"); 43 EXPECT_EQ(1, result, "other thread has its own default"); 44 EXPECT_EQ(&async, async_get_default_dispatcher(), "this thread's default is unchanged"); 45 46 async_set_default_dispatcher(nullptr); 47 END_TEST; 48} 49 50} // namespace 51 52BEGIN_TEST_CASE(default_tests) 53RUN_TEST(get_set_default_test) 54END_TEST_CASE(default_tests) 55