Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 17 | #include <sstream> |
| 18 | |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 19 | #include "common_runtime_test.h" |
| 20 | #include "reference_queue.h" |
| 21 | #include "handle_scope-inl.h" |
| 22 | #include "mirror/class-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace gc { |
| 27 | |
| 28 | class ReferenceQueueTest : public CommonRuntimeTest {}; |
| 29 | |
| 30 | TEST_F(ReferenceQueueTest, EnqueueDequeue) { |
| 31 | Thread* self = Thread::Current(); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 32 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 33 | StackHandleScope<20> hs(self); |
| 34 | Mutex lock("Reference queue lock"); |
| 35 | ReferenceQueue queue(&lock); |
| 36 | ASSERT_TRUE(queue.IsEmpty()); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 37 | ASSERT_EQ(queue.GetLength(), 0U); |
| 38 | auto ref_class = hs.NewHandle( |
| 39 | Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;", |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 40 | ScopedNullHandle<mirror::ClassLoader>())); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 41 | 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 Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 46 | queue.EnqueueReference(ref1.Get()); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 47 | ASSERT_TRUE(!queue.IsEmpty()); |
| 48 | ASSERT_EQ(queue.GetLength(), 1U); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 49 | queue.EnqueueReference(ref2.Get()); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 50 | ASSERT_TRUE(!queue.IsEmpty()); |
| 51 | ASSERT_EQ(queue.GetLength(), 2U); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 52 | |
| 53 | std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()}; |
| 54 | std::set<mirror::Reference*> dequeued; |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 55 | dequeued.insert(queue.DequeuePendingReference().Ptr()); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 56 | ASSERT_TRUE(!queue.IsEmpty()); |
| 57 | ASSERT_EQ(queue.GetLength(), 1U); |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 58 | dequeued.insert(queue.DequeuePendingReference().Ptr()); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 59 | ASSERT_EQ(queue.GetLength(), 0U); |
| 60 | ASSERT_TRUE(queue.IsEmpty()); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 61 | ASSERT_EQ(refs, dequeued); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST_F(ReferenceQueueTest, Dump) { |
| 65 | Thread* self = Thread::Current(); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 66 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 67 | StackHandleScope<20> hs(self); |
| 68 | Mutex lock("Reference queue lock"); |
| 69 | ReferenceQueue queue(&lock); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 70 | std::ostringstream oss; |
| 71 | queue.Dump(oss); |
| 72 | LOG(INFO) << oss.str(); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 73 | auto weak_ref_class = hs.NewHandle( |
| 74 | Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;", |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 75 | ScopedNullHandle<mirror::ClassLoader>())); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 76 | 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 Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 79 | ScopedNullHandle<mirror::ClassLoader>())); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 80 | 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 Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 85 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 86 | queue.EnqueueReference(ref1.Get()); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 87 | oss.str(""); |
| 88 | queue.Dump(oss); |
| 89 | LOG(INFO) << oss.str(); |
| 90 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 91 | queue.EnqueueReference(ref2.Get()); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 92 | oss.str(""); |
| 93 | queue.Dump(oss); |
| 94 | LOG(INFO) << oss.str(); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace gc |
| 98 | } // namespace art |