xref: /aosp_15_r20/art/runtime/gc/reference_queue_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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       FindClass("Ljava/lang/ref/WeakReference;", ScopedNullHandle<mirror::ClassLoader>()));
46   ASSERT_TRUE(ref_class != nullptr);
47   auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
48   ASSERT_TRUE(ref1 != nullptr);
49   auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
50   ASSERT_TRUE(ref2 != nullptr);
51   queue.EnqueueReference(ref1.Get());
52   ASSERT_TRUE(!queue.IsEmpty());
53   ASSERT_EQ(queue.GetLength(), 1U);
54   queue.EnqueueReference(ref2.Get());
55   ASSERT_TRUE(!queue.IsEmpty());
56   ASSERT_EQ(queue.GetLength(), 2U);
57 
58   std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
59   std::set<mirror::Reference*> dequeued;
60   dequeued.insert(queue.DequeuePendingReference().Ptr());
61   ASSERT_TRUE(!queue.IsEmpty());
62   ASSERT_EQ(queue.GetLength(), 1U);
63   dequeued.insert(queue.DequeuePendingReference().Ptr());
64   ASSERT_EQ(queue.GetLength(), 0U);
65   ASSERT_TRUE(queue.IsEmpty());
66   ASSERT_EQ(refs, dequeued);
67 }
68 
TEST_F(ReferenceQueueTest,Dump)69 TEST_F(ReferenceQueueTest, Dump) {
70   Thread* self = Thread::Current();
71   ScopedObjectAccess soa(self);
72   StackHandleScope<20> hs(self);
73   Mutex lock("Reference queue lock");
74   ReferenceQueue queue(&lock);
75   std::ostringstream oss;
76   queue.Dump(oss);
77   LOG(INFO) << oss.str();
78   auto weak_ref_class = hs.NewHandle(
79       FindClass("Ljava/lang/ref/WeakReference;", ScopedNullHandle<mirror::ClassLoader>()));
80   ASSERT_TRUE(weak_ref_class != nullptr);
81   auto finalizer_ref_class = hs.NewHandle(
82       FindClass("Ljava/lang/ref/FinalizerReference;", ScopedNullHandle<mirror::ClassLoader>()));
83   ASSERT_TRUE(finalizer_ref_class != nullptr);
84   auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
85   ASSERT_TRUE(ref1 != nullptr);
86   auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
87   ASSERT_TRUE(ref2 != nullptr);
88 
89   queue.EnqueueReference(ref1.Get());
90   oss.str("");
91   queue.Dump(oss);
92   LOG(INFO) << oss.str();
93 
94   queue.EnqueueReference(ref2.Get());
95   oss.str("");
96   queue.Dump(oss);
97   LOG(INFO) << oss.str();
98 }
99 
100 }  // namespace gc
101 }  // namespace art
102