-
Notifications
You must be signed in to change notification settings - Fork 122
Implement ObjectArena and ArenaRef to solve JNI Global Reference Issue #1176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3acc2af
Add a skeleton for ArenaRef
dconeybe 8975580
Create big five of ArenaRef.
cherylEnkidu d30becc
Use as example to show the efficiency drops down
cherylEnkidu 8ef27ff
fix crash in DocumentReferenceInternal::Set() due to using a stale lo…
dconeybe ebc739b
Remove invalid Wrapper::ToJava() overload for FieldValue
dconeybe ddf359c
Merge branch 'main' into cheryllin/AndroidJNI2
dconeybe ce47da6
Replace Global reference in Wrapper
cherylEnkidu 476fd44
Address Feedback
cherylEnkidu 3ec8673
Possible Fix
cherylEnkidu 5db27da
Remove DebugLog
cherylEnkidu 6c3fc50
Address Feedback 2
cherylEnkidu 54c0826
Fix "Address Feedback 2"
cherylEnkidu 7e14163
Avoid throw exception in cpp destructor in the previous commit, inste…
cherylEnkidu 84cbd19
Add mutex for HashMap inside ArenaRef
cherylEnkidu b599bfa
Address feedback
cherylEnkidu e7879c5
Address feedback again
cherylEnkidu 84400cf
Add tests to prove the fix
cherylEnkidu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
firestore/integration_test_internal/src/jni/arena_ref_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 "firestore/src/jni/arena_ref.h" | ||
#include "firestore/src/android/firestore_android.h" | ||
#include "firestore/src/jni/long.h" | ||
|
||
#include "firestore_integration_test.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace jni { | ||
namespace { | ||
|
||
using ArenaRefTestAndroid = FirestoreIntegrationTest; | ||
|
||
TEST_F(ArenaRefTestAndroid, DefaultConstructorCreatesReferenceToNull) { | ||
Env env; | ||
ArenaRef arena_ref; | ||
EXPECT_EQ(arena_ref.get(env).get(), nullptr); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, ConstructFromEnvAndObject) { | ||
Env env; | ||
Local<String> string = env.NewStringUtf("hello world"); | ||
|
||
ArenaRef arena_ref(env, string); | ||
EXPECT_TRUE(arena_ref.get(env).Equals(env, string)); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, CopysReferenceToNull) { | ||
Env env; | ||
|
||
ArenaRef arena_ref1; | ||
ArenaRef arena_ref2(arena_ref1); | ||
EXPECT_EQ(arena_ref2.get(env).get(), nullptr); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, CopysReferenceToValidObject) { | ||
Env env; | ||
|
||
Local<String> string = env.NewStringUtf("hello world"); | ||
|
||
ArenaRef arena_ref1(env, string); | ||
ArenaRef arena_ref2(arena_ref1); | ||
|
||
EXPECT_TRUE(arena_ref1.get(env).Equals(env, string)); | ||
EXPECT_TRUE(arena_ref2.get(env).Equals(env, string)); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, CopyAssignsReferenceToNull) { | ||
Env env; | ||
|
||
ArenaRef arena_ref1, arena_ref2; | ||
arena_ref2 = arena_ref1; | ||
EXPECT_EQ(arena_ref1.get(env).get(), nullptr); | ||
EXPECT_EQ(arena_ref2.get(env).get(), nullptr); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, CopyAssignsReferenceToValidObject) { | ||
Env env; | ||
|
||
Local<String> string1 = env.NewStringUtf("hello world"); | ||
Local<String> string2 = env.NewStringUtf("hello earth"); | ||
|
||
ArenaRef arena_ref1; | ||
ArenaRef arena_ref2(env, string1); | ||
ArenaRef arena_ref3(env, string2); | ||
arena_ref3 = arena_ref2; | ||
arena_ref2 = arena_ref2; | ||
|
||
EXPECT_TRUE(arena_ref3.get(env).Equals(env, string1)); | ||
EXPECT_TRUE(arena_ref2.get(env).Equals(env, string1)); | ||
|
||
arena_ref2 = arena_ref1; | ||
EXPECT_EQ(arena_ref2.get(env).get(), nullptr); | ||
EXPECT_TRUE(arena_ref3.get(env).Equals(env, string1)); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, MovesReferenceToNull) { | ||
Env env; | ||
|
||
ArenaRef arena_ref1; | ||
ArenaRef arena_ref2(std::move(arena_ref1)); | ||
EXPECT_EQ(arena_ref2.get(env).get(), nullptr); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, MovesReferenceToValidObject) { | ||
Env env; | ||
|
||
Local<String> string = env.NewStringUtf("hello world"); | ||
|
||
ArenaRef arena_ref2(env, string); | ||
ArenaRef arena_ref3(std::move(arena_ref2)); | ||
EXPECT_TRUE(arena_ref3.get(env).Equals(env, string)); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, MoveAssignsReferenceToNull) { | ||
Env env; | ||
|
||
ArenaRef arena_ref1, arena_ref2; | ||
arena_ref2 = std::move(arena_ref1); | ||
EXPECT_EQ(arena_ref2.get(env).get(), nullptr); | ||
} | ||
|
||
TEST_F(ArenaRefTestAndroid, MoveAssignsReferenceToValidObject) { | ||
Env env; | ||
|
||
Local<String> string1 = env.NewStringUtf("hello world"); | ||
Local<String> string2 = env.NewStringUtf("hello earth"); | ||
|
||
ArenaRef arena_ref1; | ||
ArenaRef arena_ref2(env, string1); | ||
arena_ref2 = std::move(arena_ref2); | ||
EXPECT_TRUE(arena_ref2.get(env).Equals(env, string1)); | ||
|
||
ArenaRef arena_ref3(env, string2); | ||
arena_ref3 = std::move(arena_ref2); | ||
EXPECT_TRUE(arena_ref3.get(env).Equals(env, string1)); | ||
|
||
arena_ref3 = std::move(arena_ref1); | ||
EXPECT_EQ(arena_ref3.get(env).get(), nullptr); | ||
} | ||
|
||
} // namespace | ||
} // namespace jni | ||
} // namespace firestore | ||
} // namespace firebase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,18 @@ TEST_F(EnvTest, CallsVoidMethods) { | |
EXPECT_EQ(length, 42); | ||
} | ||
|
||
TEST_F(EnvTest, CallsValidArenaRef) { | ||
Local<String> str = env().NewStringUtf("Foo"); | ||
ArenaRef arena_ref(env(), str); | ||
|
||
Local<Class> clazz = env().FindClass("java/lang/String"); | ||
jmethodID to_lower_case = | ||
env().GetMethodId(clazz, "toLowerCase", "()Ljava/lang/String;"); | ||
|
||
Local<Object> result = env().Call(arena_ref, Method<Object>(to_lower_case)); | ||
EXPECT_EQ("foo", result.ToString(env())); | ||
} | ||
|
||
TEST_F(EnvTest, GetsStaticFields) { | ||
Local<Class> clazz = env().FindClass("java/lang/String"); | ||
jfieldID comparator = env().GetStaticFieldId(clazz, "CASE_INSENSITIVE_ORDER", | ||
|
@@ -160,6 +172,13 @@ TEST_F(EnvTest, ToString) { | |
EXPECT_EQ("Foo", result); | ||
} | ||
|
||
TEST_F(EnvTest, IsInstanceOfChecksValidArenaRef) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Augment the |
||
Local<Class> clazz = env().FindClass("java/lang/String"); | ||
Local<String> str = env().NewStringUtf("Foo"); | ||
ArenaRef arena_ref(env(), str); | ||
EXPECT_TRUE(env().IsInstanceOf(arena_ref, clazz)); | ||
} | ||
|
||
TEST_F(EnvTest, Throw) { | ||
Local<Class> clazz = env().FindClass("java/lang/Exception"); | ||
jmethodID ctor = env().GetMethodId(clazz, "<init>", "(Ljava/lang/String;)V"); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for calling get() with a pending exception.