blob: 3ca33535623137dd8c0e07dc679e7508fcadf21d [file] [log] [blame]
Mathieu Chartier9e2094f2014-12-11 18:43:48 -08001/*
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
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070017#include <sstream>
18
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080019#include "common_runtime_test.h"
20#include "reference_queue.h"
21#include "handle_scope-inl.h"
22#include "mirror/class-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "scoped_thread_state_change-inl.h"
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080024
25namespace art {
26namespace gc {
27
28class ReferenceQueueTest : public CommonRuntimeTest {};
29
30TEST_F(ReferenceQueueTest, EnqueueDequeue) {
31 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070032 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080033 StackHandleScope<20> hs(self);
34 Mutex lock("Reference queue lock");
35 ReferenceQueue queue(&lock);
36 ASSERT_TRUE(queue.IsEmpty());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080037 ASSERT_EQ(queue.GetLength(), 0U);
38 auto ref_class = hs.NewHandle(
39 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080040 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080041 ASSERT_TRUE(ref_class.Get() != nullptr);
42 auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
43 ASSERT_TRUE(ref1.Get() != nullptr);
44 auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
45 ASSERT_TRUE(ref2.Get() != nullptr);
Richard Uhlerc4695df2016-01-15 14:08:05 -080046 queue.EnqueueReference(ref1.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080047 ASSERT_TRUE(!queue.IsEmpty());
48 ASSERT_EQ(queue.GetLength(), 1U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080049 queue.EnqueueReference(ref2.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080050 ASSERT_TRUE(!queue.IsEmpty());
51 ASSERT_EQ(queue.GetLength(), 2U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080052
53 std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
54 std::set<mirror::Reference*> dequeued;
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070055 dequeued.insert(queue.DequeuePendingReference().Ptr());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080056 ASSERT_TRUE(!queue.IsEmpty());
57 ASSERT_EQ(queue.GetLength(), 1U);
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070058 dequeued.insert(queue.DequeuePendingReference().Ptr());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080059 ASSERT_EQ(queue.GetLength(), 0U);
60 ASSERT_TRUE(queue.IsEmpty());
Richard Uhlerc4695df2016-01-15 14:08:05 -080061 ASSERT_EQ(refs, dequeued);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080062}
63
64TEST_F(ReferenceQueueTest, Dump) {
65 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070066 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080067 StackHandleScope<20> hs(self);
68 Mutex lock("Reference queue lock");
69 ReferenceQueue queue(&lock);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070070 std::ostringstream oss;
71 queue.Dump(oss);
72 LOG(INFO) << oss.str();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080073 auto weak_ref_class = hs.NewHandle(
74 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080075 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080076 ASSERT_TRUE(weak_ref_class.Get() != nullptr);
77 auto finalizer_ref_class = hs.NewHandle(
78 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080079 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080080 ASSERT_TRUE(finalizer_ref_class.Get() != nullptr);
81 auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
82 ASSERT_TRUE(ref1.Get() != nullptr);
83 auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
84 ASSERT_TRUE(ref2.Get() != nullptr);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070085
Richard Uhlerc4695df2016-01-15 14:08:05 -080086 queue.EnqueueReference(ref1.Get());
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070087 oss.str("");
88 queue.Dump(oss);
89 LOG(INFO) << oss.str();
90
Richard Uhlerc4695df2016-01-15 14:08:05 -080091 queue.EnqueueReference(ref2.Get());
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070092 oss.str("");
93 queue.Dump(oss);
94 LOG(INFO) << oss.str();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080095}
96
97} // namespace gc
98} // namespace art