1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sstream>
18
19 #include "common_runtime_test.h"
20 #include "handle_scope-inl.h"
21 #include "mirror/class-alloc-inl.h"
22 #include "mirror/class-inl.h"
23 #include "reference_queue.h"
24 #include "scoped_thread_state_change-inl.h"
25
26 namespace art HIDDEN {
27 namespace gc {
28
29 class ReferenceQueueTest : public CommonRuntimeTest {
30 protected:
ReferenceQueueTest()31 ReferenceQueueTest() {
32 use_boot_image_ = true; // Make the Runtime creation cheaper.
33 }
34 };
35
TEST_F(ReferenceQueueTest,EnqueueDequeue)36 TEST_F(ReferenceQueueTest, EnqueueDequeue) {
37 Thread* self = Thread::Current();
38 ScopedObjectAccess soa(self);
39 StackHandleScope<20> hs(self);
40 Mutex lock("Reference queue lock");
41 ReferenceQueue queue(&lock);
42 ASSERT_TRUE(queue.IsEmpty());
43 ASSERT_EQ(queue.GetLength(), 0U);
44 auto ref_class = hs.NewHandle(
45 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
46 ScopedNullHandle<mirror::ClassLoader>()));
47 ASSERT_TRUE(ref_class != nullptr);
48 auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
49 ASSERT_TRUE(ref1 != nullptr);
50 auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
51 ASSERT_TRUE(ref2 != nullptr);
52 queue.EnqueueReference(ref1.Get());
53 ASSERT_TRUE(!queue.IsEmpty());
54 ASSERT_EQ(queue.GetLength(), 1U);
55 queue.EnqueueReference(ref2.Get());
56 ASSERT_TRUE(!queue.IsEmpty());
57 ASSERT_EQ(queue.GetLength(), 2U);
58
59 std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
60 std::set<mirror::Reference*> dequeued;
61 dequeued.insert(queue.DequeuePendingReference().Ptr());
62 ASSERT_TRUE(!queue.IsEmpty());
63 ASSERT_EQ(queue.GetLength(), 1U);
64 dequeued.insert(queue.DequeuePendingReference().Ptr());
65 ASSERT_EQ(queue.GetLength(), 0U);
66 ASSERT_TRUE(queue.IsEmpty());
67 ASSERT_EQ(refs, dequeued);
68 }
69
TEST_F(ReferenceQueueTest,Dump)70 TEST_F(ReferenceQueueTest, Dump) {
71 Thread* self = Thread::Current();
72 ScopedObjectAccess soa(self);
73 StackHandleScope<20> hs(self);
74 Mutex lock("Reference queue lock");
75 ReferenceQueue queue(&lock);
76 std::ostringstream oss;
77 queue.Dump(oss);
78 LOG(INFO) << oss.str();
79 auto weak_ref_class = hs.NewHandle(
80 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
81 ScopedNullHandle<mirror::ClassLoader>()));
82 ASSERT_TRUE(weak_ref_class != nullptr);
83 auto finalizer_ref_class = hs.NewHandle(
84 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
85 ScopedNullHandle<mirror::ClassLoader>()));
86 ASSERT_TRUE(finalizer_ref_class != nullptr);
87 auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
88 ASSERT_TRUE(ref1 != nullptr);
89 auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
90 ASSERT_TRUE(ref2 != nullptr);
91
92 queue.EnqueueReference(ref1.Get());
93 oss.str("");
94 queue.Dump(oss);
95 LOG(INFO) << oss.str();
96
97 queue.EnqueueReference(ref2.Get());
98 oss.str("");
99 queue.Dump(oss);
100 LOG(INFO) << oss.str();
101 }
102
103 } // namespace gc
104 } // namespace art
105