You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2022/11/21 08:53:26 UTC

[lucenenet] 06/08: Lucene.Net.Tests.Store.TestRAMDirectory: Fixed teardown to retry file deletion if they are locked by another process.

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit b423f2b1f20599db415b5a4dc02ab43a71bd61eb
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Nov 21 11:17:15 2022 +0700

    Lucene.Net.Tests.Store.TestRAMDirectory: Fixed teardown to retry file deletion if they are locked by another process.
---
 src/Lucene.Net.Tests/Store/TestRAMDirectory.cs | 32 +++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/Lucene.Net.Tests/Store/TestRAMDirectory.cs b/src/Lucene.Net.Tests/Store/TestRAMDirectory.cs
index 2ec2ce34e..58d11a2e9 100644
--- a/src/Lucene.Net.Tests/Store/TestRAMDirectory.cs
+++ b/src/Lucene.Net.Tests/Store/TestRAMDirectory.cs
@@ -1,8 +1,10 @@
 using J2N.Threading;
 using Lucene.Net.Documents;
 using Lucene.Net.Index.Extensions;
+using Lucene.Net.Support.IO;
 using NUnit.Framework;
 using System;
+using System.Collections.Generic;
 using System.IO;
 using Assert = Lucene.Net.TestFramework.Assert;
 
@@ -202,9 +204,37 @@ namespace Lucene.Net.Store
         private void RmDir(DirectoryInfo dir)
         {
             FileInfo[] files = dir.GetFiles();
+            List<FileInfo> retryFiles = null;
             for (int i = 0; i < files.Length; i++)
             {
-                files[i].Delete();
+                try
+                {
+                    files[i].Delete();
+                }
+                catch (IOException)
+                {
+                    // LUCENENET specific - we can get here if Windows stil has a lock on the file. We will put it into a list to retry.
+                    if (retryFiles is null) retryFiles = new List<FileInfo>();
+                    retryFiles.Add(files[i]);
+                }
+            }
+            // LUCENENET specific - retry the deletion if it failed on the first pass
+            if (retryFiles is not null)
+            {
+                // Second pass - if this attempt doesn't work, just give up.
+                foreach (var file in retryFiles)
+                {
+                    try
+                    {
+                        file.Delete();
+                    }
+                    catch { /* ignore */ }
+                }
+                if (retryFiles.Count == 0)
+                {
+                    dir.Delete();
+                }
+                return;
             }
             dir.Delete();
         }