blob: c680fb57815bf43d5164d355c7cb39f9356bc55f [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"
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080020#include "handle_scope-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070021#include "mirror/class-alloc-inl.h"
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080022#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "reference_queue.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080025
26namespace art {
27namespace gc {
28
29class ReferenceQueueTest : public CommonRuntimeTest {};
30
31TEST_F(ReferenceQueueTest, EnqueueDequeue) {
32 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070033 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080034 StackHandleScope<20> hs(self);
35 Mutex lock("Reference queue lock");
36 ReferenceQueue queue(&lock);
37 ASSERT_TRUE(queue.IsEmpty());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080038 ASSERT_EQ(queue.GetLength(), 0U);
39 auto ref_class = hs.NewHandle(
40 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080041 ScopedNullHandle<mirror::ClassLoader>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080042 ASSERT_TRUE(ref_class != nullptr);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080043 auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080044 ASSERT_TRUE(ref1 != nullptr);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080045 auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080046 ASSERT_TRUE(ref2 != nullptr);
Richard Uhlerc4695df2016-01-15 14:08:05 -080047 queue.EnqueueReference(ref1.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080048 ASSERT_TRUE(!queue.IsEmpty());
49 ASSERT_EQ(queue.GetLength(), 1U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080050 queue.EnqueueReference(ref2.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080051 ASSERT_TRUE(!queue.IsEmpty());
52 ASSERT_EQ(queue.GetLength(), 2U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080053
54 std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
55 std::set<mirror::Reference*> dequeued;
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070056 dequeued.insert(queue.DequeuePendingReference().Ptr());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080057 ASSERT_TRUE(!queue.IsEmpty());
58 ASSERT_EQ(queue.GetLength(), 1U);
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070059 dequeued.insert(queue.DequeuePendingReference().Ptr());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080060 ASSERT_EQ(queue.GetLength(), 0U);
61 ASSERT_TRUE(queue.IsEmpty());
Richard Uhlerc4695df2016-01-15 14:08:05 -080062 ASSERT_EQ(refs, dequeued);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080063}
64
65TEST_F(ReferenceQueueTest, Dump) {
66 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070067 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080068 StackHandleScope<20> hs(self);
69 Mutex lock("Reference queue lock");
70 ReferenceQueue queue(&lock);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070071 std::ostringstream oss;
72 queue.Dump(oss);
73 LOG(INFO) << oss.str();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080074 auto weak_ref_class = hs.NewHandle(
75 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080076 ScopedNullHandle<mirror::ClassLoader>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080077 ASSERT_TRUE(weak_ref_class != nullptr);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080078 auto finalizer_ref_class = hs.NewHandle(
79 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080080 ScopedNullHandle<mirror::ClassLoader>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080081 ASSERT_TRUE(finalizer_ref_class != nullptr);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080082 auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080083 ASSERT_TRUE(ref1 != nullptr);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080084 auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080085 ASSERT_TRUE(ref2 != nullptr);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070086
Richard Uhlerc4695df2016-01-15 14:08:05 -080087 queue.EnqueueReference(ref1.Get());
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070088 oss.str("");
89 queue.Dump(oss);
90 LOG(INFO) << oss.str();
91
Richard Uhlerc4695df2016-01-15 14:08:05 -080092 queue.EnqueueReference(ref2.Get());
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070093 oss.str("");
94 queue.Dump(oss);
95 LOG(INFO) << oss.str();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080096}
97
98} // namespace gc
99} // namespace art