blob: 35bf718875efd6ee3d76978998fd469888cf4013 [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
17#include "common_runtime_test.h"
18#include "reference_queue.h"
19#include "handle_scope-inl.h"
20#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
23namespace art {
24namespace gc {
25
26class ReferenceQueueTest : public CommonRuntimeTest {};
27
28TEST_F(ReferenceQueueTest, EnqueueDequeue) {
29 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070030 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080031 StackHandleScope<20> hs(self);
32 Mutex lock("Reference queue lock");
33 ReferenceQueue queue(&lock);
34 ASSERT_TRUE(queue.IsEmpty());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080035 ASSERT_EQ(queue.GetLength(), 0U);
36 auto ref_class = hs.NewHandle(
37 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080038 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080039 ASSERT_TRUE(ref_class.Get() != nullptr);
40 auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
41 ASSERT_TRUE(ref1.Get() != nullptr);
42 auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
43 ASSERT_TRUE(ref2.Get() != nullptr);
Richard Uhlerc4695df2016-01-15 14:08:05 -080044 queue.EnqueueReference(ref1.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080045 ASSERT_TRUE(!queue.IsEmpty());
46 ASSERT_EQ(queue.GetLength(), 1U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080047 queue.EnqueueReference(ref2.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080048 ASSERT_TRUE(!queue.IsEmpty());
49 ASSERT_EQ(queue.GetLength(), 2U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080050
51 std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
52 std::set<mirror::Reference*> dequeued;
53 dequeued.insert(queue.DequeuePendingReference());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080054 ASSERT_TRUE(!queue.IsEmpty());
55 ASSERT_EQ(queue.GetLength(), 1U);
Richard Uhlerc4695df2016-01-15 14:08:05 -080056 dequeued.insert(queue.DequeuePendingReference());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080057 ASSERT_EQ(queue.GetLength(), 0U);
58 ASSERT_TRUE(queue.IsEmpty());
Richard Uhlerc4695df2016-01-15 14:08:05 -080059 ASSERT_EQ(refs, dequeued);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080060}
61
62TEST_F(ReferenceQueueTest, Dump) {
63 Thread* self = Thread::Current();
Mathieu Chartiered150002015-08-28 11:16:54 -070064 ScopedObjectAccess soa(self);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080065 StackHandleScope<20> hs(self);
66 Mutex lock("Reference queue lock");
67 ReferenceQueue queue(&lock);
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080068 queue.Dump(LOG(INFO));
69 auto weak_ref_class = hs.NewHandle(
70 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080071 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080072 ASSERT_TRUE(weak_ref_class.Get() != nullptr);
73 auto finalizer_ref_class = hs.NewHandle(
74 Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -080075 ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080076 ASSERT_TRUE(finalizer_ref_class.Get() != nullptr);
77 auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
78 ASSERT_TRUE(ref1.Get() != nullptr);
79 auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
80 ASSERT_TRUE(ref2.Get() != nullptr);
Richard Uhlerc4695df2016-01-15 14:08:05 -080081 queue.EnqueueReference(ref1.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080082 queue.Dump(LOG(INFO));
Richard Uhlerc4695df2016-01-15 14:08:05 -080083 queue.EnqueueReference(ref2.Get());
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080084 queue.Dump(LOG(INFO));
85}
86
87} // namespace gc
88} // namespace art