reference_queue_test.cc 3.35 KB
Newer Older
Mathieu Chartier's avatar
Mathieu Chartier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common_runtime_test.h"
#include "reference_queue.h"
#include "handle_scope-inl.h"
#include "mirror/class-inl.h"
#include "scoped_thread_state_change.h"

namespace art {
namespace gc {

class ReferenceQueueTest : public CommonRuntimeTest {};

TEST_F(ReferenceQueueTest, EnqueueDequeue) {
  Thread* self = Thread::Current();
30
  ScopedObjectAccess soa(self);
Mathieu Chartier's avatar
Mathieu Chartier committed
31 32 33 34 35 36 37
  StackHandleScope<20> hs(self);
  Mutex lock("Reference queue lock");
  ReferenceQueue queue(&lock);
  ASSERT_TRUE(queue.IsEmpty());
  ASSERT_EQ(queue.GetLength(), 0U);
  auto ref_class = hs.NewHandle(
      Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
38
                                                      ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier's avatar
Mathieu Chartier committed
39 40 41 42 43
  ASSERT_TRUE(ref_class.Get() != nullptr);
  auto ref1(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
  ASSERT_TRUE(ref1.Get() != nullptr);
  auto ref2(hs.NewHandle(ref_class->AllocObject(self)->AsReference()));
  ASSERT_TRUE(ref2.Get() != nullptr);
44
  queue.EnqueueReference(ref1.Get());
Mathieu Chartier's avatar
Mathieu Chartier committed
45 46
  ASSERT_TRUE(!queue.IsEmpty());
  ASSERT_EQ(queue.GetLength(), 1U);
47
  queue.EnqueueReference(ref2.Get());
Mathieu Chartier's avatar
Mathieu Chartier committed
48 49
  ASSERT_TRUE(!queue.IsEmpty());
  ASSERT_EQ(queue.GetLength(), 2U);
50 51 52 53

  std::set<mirror::Reference*> refs = {ref1.Get(), ref2.Get()};
  std::set<mirror::Reference*> dequeued;
  dequeued.insert(queue.DequeuePendingReference());
Mathieu Chartier's avatar
Mathieu Chartier committed
54 55
  ASSERT_TRUE(!queue.IsEmpty());
  ASSERT_EQ(queue.GetLength(), 1U);
56
  dequeued.insert(queue.DequeuePendingReference());
Mathieu Chartier's avatar
Mathieu Chartier committed
57 58
  ASSERT_EQ(queue.GetLength(), 0U);
  ASSERT_TRUE(queue.IsEmpty());
59
  ASSERT_EQ(refs, dequeued);
Mathieu Chartier's avatar
Mathieu Chartier committed
60 61 62 63
}

TEST_F(ReferenceQueueTest, Dump) {
  Thread* self = Thread::Current();
64
  ScopedObjectAccess soa(self);
Mathieu Chartier's avatar
Mathieu Chartier committed
65 66 67 68 69 70
  StackHandleScope<20> hs(self);
  Mutex lock("Reference queue lock");
  ReferenceQueue queue(&lock);
  queue.Dump(LOG(INFO));
  auto weak_ref_class = hs.NewHandle(
      Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/WeakReference;",
71
                                                      ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier's avatar
Mathieu Chartier committed
72 73 74
  ASSERT_TRUE(weak_ref_class.Get() != nullptr);
  auto finalizer_ref_class = hs.NewHandle(
      Runtime::Current()->GetClassLinker()->FindClass(self, "Ljava/lang/ref/FinalizerReference;",
75
                                                      ScopedNullHandle<mirror::ClassLoader>()));
Mathieu Chartier's avatar
Mathieu Chartier committed
76 77 78 79 80
  ASSERT_TRUE(finalizer_ref_class.Get() != nullptr);
  auto ref1(hs.NewHandle(weak_ref_class->AllocObject(self)->AsReference()));
  ASSERT_TRUE(ref1.Get() != nullptr);
  auto ref2(hs.NewHandle(finalizer_ref_class->AllocObject(self)->AsReference()));
  ASSERT_TRUE(ref2.Get() != nullptr);
81
  queue.EnqueueReference(ref1.Get());
Mathieu Chartier's avatar
Mathieu Chartier committed
82
  queue.Dump(LOG(INFO));
83
  queue.EnqueueReference(ref2.Get());
Mathieu Chartier's avatar
Mathieu Chartier committed
84 85 86 87 88
  queue.Dump(LOG(INFO));
}

}  // namespace gc
}  // namespace art