public void AsyncLock_ConcurrentLockFollowedByRelease() { ManualResetEvent done = new ManualResetEvent(false); const int MaxCount = 100; int count = 0; Parallel.For(0, MaxCount, async cnt => { await _lockFile.LockAsync(); // We are now within the lock await Task.Delay(5); count = count + 1; // Releasing lock _lockFile.Release(); if (count == MaxCount) { done.Set(); } }); done.WaitOne(); Assert.Equal(MaxCount, count); }
/** * Uninstall a package asynchronously. */ public async Task RemovePackageAsync(string userId, string projectName, string packageName) { var pwd = $"{GetBaseDir(userId, projectName)}"; if (!Directory.Exists(pwd)) { throw new EndUserException("The package has already been removed."); } using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { try { Process proc = StartProcess($"\"{_yarnPath}\" remove {packageName}", pwd); await WaitForExitAsync(proc); await TransformFilesAsync(pwd); string output = await proc.StandardOutput.ReadToEndAsync(); } catch (Exception e) { throw new EndUserException("Internal error occured durning the uninstall of \"{packageName}\".", e); } } }
public void AsyncLock_ThrowsIfNotInitialized() { string lockFilePath = Path.Combine(PathHelper.TestLockPath, "uninitialized.lock"); LockFile uninitialized = new LockFile(lockFilePath, NullTracerFactory.Instance); Assert.Throws <InvalidOperationException>(() => uninitialized.LockAsync()); }
async Task <UserFile> IUserFileManager.WriteAsync(string userId, string projectName, string filePath, Stream data, string contentType) { // Get the base files directory. string baseDir = GetBaseDir(userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { List <UserFile> fileMetaList = null; try { fileMetaList = await GetUserFilesAsync(userId, projectName); } catch (Exception e) { await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", "[]", Encoding.UTF8); fileMetaList = new List <UserFile>(); } if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var _)) { return(await OverwriteFileAsync(baseDir, fileMetaList, filePath, data, contentType)); } else { return(await WriteNewFileAsync(baseDir, fileMetaList, filePath, data, contentType)); } } }
async Task IUserFileManager.RenameAsync(string userId, string projectName, string oldFilePath, string newFilePath) { // Get the base files directory. string baseDir = GetBaseDir(userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { string fileMetaJson = null; try { // Read the metadata file. fileMetaJson = await AsyncIO.ReadAllTextAsync($"{baseDir}\\filemeta.json", Encoding.UTF8); } catch (FileNotFoundException e) { ThrowNotFound(projectName, oldFilePath); } // Get the file meta dict. List <UserFile> fileMetaList = JsonConvert.DeserializeObject <List <UserFile> >(fileMetaJson); if (fileMetaList.TryFind(x => x.VirtualPath == oldFilePath, out var fileMeta)) { // Update the value in the list. fileMetaList.Remove(fileMeta); fileMetaList.Add(fileMeta.WithVirtualPath(newFilePath)); } // Write the new file meta back to the file. await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", JsonConvert.SerializeObject(fileMetaList), Encoding.UTF8); } }
/** * Initializes the project if it has not already been initialized. */ private async Task TryInitProjectAsync(string pwd, string userId, string projectName) { if (!Directory.Exists(pwd)) { using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { await TryInstallToolsAsync(); Directory.CreateDirectory(pwd); await AsyncIO.WriteAllTextAsync(pwd + "\\package.json", "{\"name\":\"creo-web-" + userId + "-" + projectName + "\",\"version\":\"1.0.0\",\"dependencies\":{}}", Encoding.UTF8); } } }
async Task <IReadOnlyList <UserFile> > IUserFileManager.GetFilesAsync(string userId, string projectName) { // Get the base files directory. string baseDir = GetBaseDir(userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { try { return(await GetUserFilesAsync(userId, projectName)); } catch (FileNotFoundException e) { return(new List <UserFile>()); } } }
async Task IUserFileManager.DeleteAsync(string userId, string projectName, string filePath) { // Get the base files directory. string baseDir = GetBaseDir(userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { var fileMetaList = await GetUserFilesAsync(userId, projectName); if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var fileMeta)) { File.Delete($"{baseDir}\\{fileMeta.LocalPath}"); // Remove from the list. fileMetaList.Remove(fileMeta); } // Write the new file meta back to the file. await AsyncIO.WriteAllTextAsync($"{baseDir}\\filemeta.json", JsonConvert.SerializeObject(fileMetaList), Encoding.UTF8); } }
/** * Install a package asynchronously. */ public async Task AddPackageAsync(string userId, string projectName, string packageName) { var pwd = $"{GetBaseDir(userId, projectName)}"; await TryInitProjectAsync(pwd, userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { try { Process proc = StartProcess($"{_yarnPath} add {packageName} --exact", pwd); await WaitForExitAsync(proc); await TransformFilesAsync(pwd); string output = await proc.StandardOutput.ReadToEndAsync(); } catch (Exception e) { throw new EndUserException("Internal error occured durning the install of \"{packageName}\".", e); } } }
async Task <UserFileStream> IUserFileManager.ReadAsync(string userId, string projectName, string filePath) { // Get the base files directory. string baseDir = GetBaseDir(userId, projectName); using (await LockFile.LockAsync(GetLockFile(userId, projectName))) { var fileMetaList = await GetUserFilesAsync(userId, projectName); if (fileMetaList.TryFind(x => x.VirtualPath == filePath, out var fileMeta)) { return(new UserFileStream ( stream: File.OpenRead($"{baseDir}\\{fileMeta.LocalPath}"), metaData: fileMeta )); } else { ThrowNotFound(projectName, filePath); throw null; } } }