Skip to content

Commit a834eca

Browse files
committed
Fix flaky test caused by the dependency of arena_ref.cc been destroyed before the destructor of ArenaRef was called
1 parent 84cbd19 commit a834eca

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

firestore/src/jni/arena_ref.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "firestore/src/jni/arena_ref.h"
1818

1919
#include <atomic>
20+
#include <utility>
2021

2122
#include "firestore/src/android/firestore_android.h"
2223
#include "firestore/src/jni/env.h"
@@ -31,6 +32,9 @@ namespace jni {
3132
namespace {
3233

3334
HashMap* gArenaRefHashMap = nullptr;
35+
jclass gHashMapClass = nullptr;
36+
jclass gLongClass = nullptr;
37+
jmethodID gLongConstructor = nullptr;
3438
std::mutex mutex_;
3539

3640
int64_t GetNextArenaRefKey() {
@@ -115,7 +119,8 @@ ArenaRef::~ArenaRef() {
115119
}
116120

117121
Local<Long> ArenaRef::key_object(Env& env) const {
118-
return Long::Create(env, key_);
122+
jobject key = env.get()->NewObject(gLongClass, gLongConstructor, key_);
123+
return {env.get(), key};
119124
}
120125

121126
void ArenaRef::Initialize(Env& env) {
@@ -124,6 +129,13 @@ void ArenaRef::Initialize(Env& env) {
124129
}
125130
Global<HashMap> hash_map(HashMap::Create(env));
126131
gArenaRefHashMap = new HashMap(hash_map.release());
132+
133+
gHashMapClass = env.get()->FindClass("java/util/HashMap");
134+
gHashMapClass = static_cast<jclass>(env.get()->NewGlobalRef(gHashMapClass));
135+
136+
gLongClass = env.get()->FindClass("java/lang/Long");
137+
gLongClass = static_cast<jclass>(env.get()->NewGlobalRef(gLongClass));
138+
gLongConstructor = env.get()->GetMethodID(gLongClass, "<init>", "(J)V");
127139
}
128140

129141
Local<Object> ArenaRef::get(Env& env) const {

0 commit comments

Comments
 (0)