Skip to content
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

Apply java.nio.file.Files.createDirectories for Android SDK 26 or later to surface more error info #103

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f65f8d2
Update the minimum yml changes to make ci working
TonyTangAndroid Sep 15, 2024
27ed6e7
Update the minimum gradle changes to support android api 30 Robolectr…
TonyTangAndroid Sep 15, 2024
e643582
Add rxjava2 as test dependencies
TonyTangAndroid Sep 15, 2024
16bb64b
Add more SimpleStore and AtomicFile integration test
TonyTangAndroid Sep 15, 2024
dbad1e5
Fix the race condition issue on creating the parent folder.
TonyTangAndroid Sep 15, 2024
3997ec0
Add AtomicFileConcurrentTest.kt
TonyTangAndroid Sep 15, 2024
5cc34ad
Add AtomicFileConcurrentTest.kt that reproducing the failed exception
TonyTangAndroid Sep 15, 2024
8685557
Bump up the ci
TonyTangAndroid Sep 15, 2024
8c1bc76
Use `./gradlew spotlessApply` to fix the file format.
TonyTangAndroid Sep 15, 2024
24f5f0f
Fix the release ci
TonyTangAndroid Sep 15, 2024
cfe78e0
Fix the main ci
TonyTangAndroid Sep 15, 2024
09c9fb8
Fix the main ci
TonyTangAndroid Sep 15, 2024
2ef8720
Fix the main ci by bumping down malinskiy/action-android
TonyTangAndroid Sep 15, 2024
115d2ef
Fix the main ci by using RIBs
TonyTangAndroid Sep 15, 2024
1733e14
Fix the main ci by using pure RIBs script
TonyTangAndroid Sep 15, 2024
fd3fa2f
Only apply to pull request
TonyTangAndroid Sep 15, 2024
1419aa7
Make the ConcurrentTestUtil more intensive
TonyTangAndroid Sep 15, 2024
a677929
Ignore the flaky test
TonyTangAndroid Sep 15, 2024
0438626
Refine the test comment
TonyTangAndroid Sep 15, 2024
e68a3a9
Keep original changes.
TonyTangAndroid Sep 15, 2024
faf1391
Apply the minimum fix with test code
TonyTangAndroid Sep 15, 2024
9446f54
Apply java.nio.file.Files.createDirectories for Android SDK 26 or lat…
TonyTangAndroid Sep 16, 2024
bf929c9
Revert "Apply java.nio.file.Files.createDirectories for Android SDK 2…
TonyTangAndroid Sep 16, 2024
790a1f2
Revert "Apply the minimum fix with test code"
TonyTangAndroid Sep 16, 2024
5b77bd7
resolveParentFolder with Files.createDirectories without concurrent fix
TonyTangAndroid Sep 16, 2024
c2eae06
Revert "resolveParentFolder with Files.createDirectories without conc…
TonyTangAndroid Sep 16, 2024
f9100b1
resolveParentFolder with Files.createDirectories without concurrent fix
TonyTangAndroid Sep 16, 2024
033d384
Add boilerplate test code
TonyTangAndroid Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ def androidx = [
]

def test = [
concurrentunit : 'net.jodah:concurrentunit:0.4.6',
junit : 'junit:junit:4.13.1',
junitX : 'androidx.test.ext:junit:1.0.0',
truth : 'com.google.truth:truth:1.1',
truthX : 'androidx.test.ext:truth:1.5.0',
robolectric: 'org.robolectric:robolectric:4.4',
robolectric: 'org.robolectric:robolectric:4.5.1',
espresso : 'androidx.test.espresso:espresso-core:3.5.1',
runner : 'androidx.test:runner:1.5.2',
rules : 'androidx.test:rules:1.5.0',
Expand Down
4 changes: 4 additions & 0 deletions simplestore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ android {

testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests {
includeAndroidResources = true
}
}

defaultConfig {
Expand All @@ -45,6 +48,7 @@ dependencies {
testImplementation deps.test.junitX
testImplementation deps.test.truth
testImplementation deps.test.truthX
testImplementation deps.test.concurrentunit
testImplementation deps.test.robolectric

androidTestImplementation deps.test.runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jetbrains.annotations.Nullable;

/*
Expand Down Expand Up @@ -93,10 +96,7 @@ public FileOutputStream startWrite() throws IOException {
try {
return new FileOutputStream(mNewName);
} catch (FileNotFoundException e) {
File parent = mNewName.getParentFile();
if (!parent.mkdirs()) {
throw new IOException("Failed to create directory for " + mNewName);
}
resolveParentFolder(e);
try {
return new FileOutputStream(mNewName);
} catch (FileNotFoundException e2) {
Expand All @@ -105,6 +105,22 @@ public FileOutputStream startWrite() throws IOException {
}
}

private void resolveParentFolder(FileNotFoundException rawError) throws IOException {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are minimum changes without introducing actual fix but to collect more information.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Path parentPath = mNewName.toPath().getParent();
if (parentPath != null && !Files.exists(parentPath)) {
Files.createDirectories(parentPath);
} else {
throw rawError;
}
} else {
File parent = mNewName.getParentFile();
if (!parent.mkdirs()) {
throw rawError;
}
}
}

/**
* Call when you have successfully finished writing to the stream returned by {@link
* #startWrite()}. This will close, sync, and commit the new data. The next attempt to read the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2019. Uber Technologies
*
* 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.
*/
package com.uber.simplestore.impl

import android.app.Application
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.uber.simplestore.impl.ConcurrentTestUtil.executeConcurrent
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.File
import java.io.FileNotFoundException


@RunWith(RobolectricTestRunner::class)
class AtomicFileConcurrentTest {

@Test(expected = FileNotFoundException::class)
fun `case 0 when parent folder is absent will trigger FileNotFoundException`() {
//arrange
//makeParentFolder()
val targetFile = createTargetFile()
//act
val target = targetFile.outputStream()
//assert
assertThat(target).isNotNull()
}

@Test
fun `case 1 when parent folder is present will not trigger FileNotFoundException`() {
//arrange
makeParentFolder()
val targetFile = createTargetFile()
//act
val target = targetFile.outputStream()
//assert
assertThat(target).isNotNull()
}


/**
* This represents a typical file that stores a value for a random key.
*/
private fun createTargetFile(): File {
return File(app().dataDir, "files/simplestore/657b3cd7-f689-451b-aca0-628de60aa234/random_key")
}

/**
* This presents a typical simple store folder scoped with under an `uuid`.
*/
private fun makeParentFolder() {
val parent = File(app().dataDir, "files/simplestore/657b3cd7-f689-451b-aca0-628de60aa234")
parent.mkdirs()
}

private fun app(): Application {
return ApplicationProvider.getApplicationContext()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2019. Uber Technologies
*
* 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.
*/
package com.uber.simplestore.impl

import net.jodah.concurrentunit.Waiter
import java.util.concurrent.Executors


/**
* A utility class to execute a given action concurrently.
* It will trigger [Waiter.fail] if the action throws an exception during the whole concurrent execution process.
* It will trigger [Waiter.resume] if the action is concluded successfully for the whole concurrent execution.
*/
object ConcurrentTestUtil {
/**
* The maximum number of threads to be executed concurrently.
*/
private const val MAX_THREAD = 5


/**
* Executes the given action concurrently.
* It will trigger [Waiter.fail] if the action throws an exception during the whole concurrent execution process.
* It will trigger [Waiter.resume] if the action is concluded successfully for the whole concurrent execution.
*/
fun executeConcurrent(action: () -> Unit) {
val waiter = Waiter()
for (i in 0 until 1000) {
val service = Executors.newFixedThreadPool(MAX_THREAD)
for (j in 0 until 10) {
service.submit {
executeConcurrentInternally(action, waiter)
}
}
}
waiter.await(1000L * MAX_THREAD)
}

private fun executeConcurrentInternally(action: () -> Unit, waiter: Waiter) {
try {
run(action)
} catch (e: Exception) {
waiter.fail(e)
} finally {
waiter.resume()
}
}


}