1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.file.common; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import android.content.Context; 22 import android.net.Uri; 23 import androidx.test.core.app.ApplicationProvider; 24 import com.google.android.libraries.mobiledatadownload.file.backends.FileUriAdapter; 25 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 26 import com.google.common.io.Files; 27 import java.io.File; 28 import java.io.FileOutputStream; 29 import java.io.IOException; 30 import java.util.concurrent.ConcurrentHashMap; 31 import java.util.concurrent.ConcurrentMap; 32 import java.util.concurrent.Semaphore; 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.junit.runners.JUnit4; 37 38 @RunWith(JUnit4.class) 39 public class LockScopeTest { 40 41 // Keys to message data sent between main and service processes 42 private static final String URI_BUNDLE_KEY_1 = "uri1"; 43 private static final String URI_BUNDLE_KEY_2 = "uri2"; 44 45 @Rule public final TemporaryUri tmpUri = new TemporaryUri(); 46 47 private final Context mainContext = ApplicationProvider.getApplicationContext(); 48 49 @Test createWithSharedThreadLocks_sharesThreadLocksAcrossInstances()50 public void createWithSharedThreadLocks_sharesThreadLocksAcrossInstances() throws IOException { 51 ConcurrentMap<String, Semaphore> lockMap = new ConcurrentHashMap<>(); 52 LockScope lockScope = LockScope.createWithExistingThreadLocks(lockMap); 53 LockScope otherLockScope = LockScope.createWithExistingThreadLocks(lockMap); 54 Uri uri = tmpUri.newUri(); 55 56 try (Lock lock = lockScope.threadLock(uri)) { 57 assertThat(otherLockScope.tryThreadLock(uri)).isNull(); 58 } 59 60 assertThat(otherLockScope.tryThreadLock(uri)).isNotNull(); 61 } 62 63 @Test createWithFailingThreadLocks_willFailToAcquireThreadLocks()64 public void createWithFailingThreadLocks_willFailToAcquireThreadLocks() throws IOException { 65 LockScope lockScope = LockScope.createWithFailingThreadLocks(); 66 Uri uri = tmpUri.newUri(); 67 68 assertThrows(UnsupportedFileStorageOperation.class, () -> lockScope.threadLock(uri)); 69 assertThat(lockScope.tryThreadLock(uri)).isNull(); 70 } 71 72 @Test createFileLockSucceedsInSingleProcess()73 public void createFileLockSucceedsInSingleProcess() throws Exception { 74 LockScope lockScope = LockScope.create(); 75 Uri uri = tmpUri.newUri(); 76 77 try (FileOutputStream stream = getStreamFromUri(uri); 78 Lock lock = lockScope.fileLock(stream.getChannel(), /* shared= */ false)) { 79 assertThat(lock).isNotNull(); 80 } 81 } 82 getStreamFromUri(Uri uri)83 private static FileOutputStream getStreamFromUri(Uri uri) throws IOException { 84 File file = FileUriAdapter.instance().toFile(uri); 85 Files.createParentDirs(file); 86 return new FileOutputStream(file); 87 } 88 } 89