Parallel comparison running

Hi Guys,

I am facing interesting situation when I am trying to run my tests in parallel using GroupDocs.Comparison for comparing files.

If I am running code below within my tests (that are running in parallel)

 public void IsSame(string sourceFile, string fileToCompare)
 {
        using (Comparer comparer = new Comparer(sourceFile))
       {
            comparer.Add(fileToCompare);
            comparer.Compare(
                   "Report.pdf",
                    new CompareOptions
                    {
                        DetalisationLevel = DetalisationLevel.High,
                        DetectStyleChanges = true,
                    });
        }
  }

I am getting following exception:

\u0006 : Object reference not set to an instance of an object… Comparison cannot create result document.

Stack trace

[at \u0005 .SaveDocument(Stream \u0002, Document \u0003, Document \u0005, SaveOptions \u0008)]
[at GroupDocs.Comparison.Comparer.\u0002(Stream \u0002, SaveOptions \u0003)]
GroupDocs.Comparison.Comparer.%2Fu0002&version=GBmain&_a=contents&line=1&lineEnd=2&lineStartColumn=1&lineEndColumn=1&lineStyle=plain)
at GroupDocs.Comparison.Comparer.Compare(Stream document, SaveOptions saveOptions, CompareOptions compareOptions)
at GroupDocs.Comparison.Comparer.Compare(String filePath, CompareOptions compareOptions)

However if I am running code below (with lock) in parallel it does work for me without exceptions:

        private static readonly object lockObj = new();      
        
        public void IsSame(string sourceFile, string fileToCompare)
        {
            using (Comparer comparer = new Comparer(sourceFile))
            {
                comparer.Add(fileToCompare);
                lock (lockObj)
                {
                    comparer.Compare(
                           "Report.pdf",
                            new CompareOptions
                            {
                                DetalisationLevel = DetalisationLevel.High,
                                DetectStyleChanges = true,
                            });
                }
            }
        }

So I guess question is if GroupDocs.Comparsion is thread safe and should allow me to run comparison in parallel, if yes, maybe you could suggest why it does not work for me, please?

Thanks in advance.

1 Like

@usernamename

Yes. It is.

Could you please share a sample application using that issue could be reproduced (along-with sample files)?
Also specify the GroupDocs.Comparison for .NET API version that you are using.

@atir.tahir Thank you for a quick response.

“GroupDocs.Comparison” Version=“23.3.0”

I am also attaching example of simple solution (TestProject.zip) with the same issue.
If I run tests NOT in parallel tests are working as expected (assertion fails as files are not the same).

However if I run them in parallel I am getting following:

Message:
: Object reference not set to an instance of an object… Comparison cannot create result document.

Stack Trace:
.()
.SaveDocument(Stream , Document , Document , SaveOptions )
Comparer.(Stream , SaveOptions )
Comparer.Compare(Stream document, SaveOptions saveOptions, CompareOptions compareOptions)
Comparer.Compare(String filePath, CompareOptions compareOptions)
Tests.SameFiles(String source, String compare, String report) line 59
Tests.Test1() line 22

Please let me know if any additional information is needed.

1 Like

@usernamename
Thanks for sharing the details. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): COMPARISONNET-3443

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Hi @atir.tahir, Guys,

Is there any news on issue mentioned above.
As we are running our tests in parallel, it is quite important for us to understand if library can be used as in example attached above (without throwing exception).

Thanks in advance.

@usernamename

A single-threaded program is thread-safe by definition, since it uses only one thread of code execution and there is no need to control access to shared resources by multiple threads. In this case there are no problems with synchronization of access to shared data, because a thread blocks access of other threads until it finishes its work with this data. If you run comparison several times in parallel, access to shared data is blocked by one of the processes. And using “lock” solves the problem of synchronizing access to this data.