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 {
27 namespace gc {
28 
29 class ReferenceQueueTest : public CommonRuntimeTest {};
30 
TEST_F(ReferenceQueueTest,EnqueueDequeue)31 TEST_F(ReferenceQueueTest, EnqueueDequeue) {
32   Thread* self = Thread::Current();
33   ScopedObjectAccess soa(self);
34   StackHandleScope<20> hs(self);
35   Mutex lock("Reference queue lock");
36   ReferenceQueue queue(&lock);
37   ASSERT_TRUE(queue.IsEmpty());
38   ASSERT_EQ(queue.GetLength(), 0U);
39   auto ref_class = hs.NewHandle(
40       Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
41                                                       ScopedNullHandle<mirror::ClassLoader>()));
42   ASSERT_TRUE(ref_class != nullptr);
43   auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
44   ASSERT_TRUE(ref1 != nullptr);
45   auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
46   ASSERT_TRUE(ref2 != nullptr);
47   queue.EnqueueReference(ref1.Get());
48   ASSERT_TRUE(!queue.IsEmpty());
49   ASSERT_EQ(queue.GetLength(), 1U);
50   queue.EnqueueReference(ref2.Get());
51   ASSERT_TRUE(!queue.IsEmpty());
52   ASSERT_EQ(queue.GetLength(), 2U);
53 
54   std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
55   std::set<mirror::Reference*> dequeued;
56   dequeued.insert(queue.DequeuePendingReference().Ptr());
57   ASSERT_TRUE(!queue.IsEmpty());
58   ASSERT_EQ(queue.GetLength(), 1U);
59   dequeued.insert(queue.DequeuePendingReference().Ptr());
60   ASSERT_EQ(queue.GetLength(), 0U);
61   ASSERT_TRUE(queue.IsEmpty());
62   ASSERT_EQ(refs, dequeued);
63 }
64 
TEST_F(ReferenceQueueTest,Dump)65 TEST_F(ReferenceQueueTest, Dump) {
66   Thread* self = Thread::Current();
67   ScopedObjectAccess soa(self);
68   StackHandleScope<20> hs(self);
69   Mutex lock("Reference queue lock");
70   ReferenceQueue queue(&lock);
71   std::ostringstream oss;
72   queue.Dump(oss);
73   LOG(INFO) << oss.str();
74   auto weak_ref_class = hs.NewHandle(
75       Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
76                                                       ScopedNullHandle<mirror::ClassLoader>()));
77   ASSERT_TRUE(weak_ref_class != nullptr);
78   auto finalizer_ref_class = hs.NewHandle(
79       Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
80                                                       ScopedNullHandle<mirror::ClassLoader>()));
81   ASSERT_TRUE(finalizer_ref_class != nullptr);
82   auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
83   ASSERT_TRUE(ref1 != nullptr);
84   auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
85   ASSERT_TRUE(ref2 != nullptr);
86 
87   queue.EnqueueReference(ref1.Get());
88   oss.str("");
89   queue.Dump(oss);
90   LOG(INFO) << oss.str();
91 
92   queue.EnqueueReference(ref2.Get());
93   oss.str("");
94   queue.Dump(oss);
95   LOG(INFO) << oss.str();
96 }
97 
98 }  // namespace gc
99 }  // namespace art
100