Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

ExclusivelyUsesAttribute vs InclusivelyUsesAttribute
hacker_112
#1 Posted : Wednesday, September 4, 2013 6:57:29 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/4/2013(UTC)
Posts: 2

Thanks: 1 times
Can anyone explain to me what the difference is between ExclusivelyUsesAttribute and InclusivelyUsesAttribute ( seen in http://www.ncrunch.net/d...time-framework_overview )?
I have read the documentation but I can not find what the difference is, and when to use the first, when to use the second and when to use both.

Could someone give me an (code) example on when to use ExclusivelyUsesAttribute, when to use InclusivelyUsesAttribute and when to use both?

Best regards,
David
Remco
#2 Posted : Wednesday, September 4, 2013 7:22:36 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,987

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi David -

Tests marked with InclusivelyUsesAttribute can still be run together if they are using the same resource.

Tests marked with ExclusivelyUsesAttribute cannot be run together if they use the same resource.

InclusivelyUsesAttribute is completely useless unless you use it in combination with ExclusivelyUsesAttribute, as 'inclusively' using a resource means that you have no problem with others making use of a resource, provided they aren't using it exclusively. Scenarios where InclusivelyUsesAttribute is needed are quite rare and probably would only appear in fairly complex cases - usually around database or file I/O.

See the following example:

Code:
using System.IO;
using NCrunch.Framework;
using NUnit.Framework;

public class MyLoggingFixture
{
	[Test]
	[InclusivelyUses("LoggingDirectory")]
	public void FirstTestThatWritesToALogFile()
	{
		Directory.CreateDirectory("LoggingDirectory");

		using (var file = new StreamWriter(@"LoggingDirectory\MyLogFile.log"))
		{
			file.WriteLine("Some stuff written to a log file in the log directory");
		}
	}

	[Test]
	[InclusivelyUses("LoggingDirectory")]
	public void SecondTestThatWritesToALogFile()
	{
		Directory.CreateDirectory("LoggingDirectory");

		using (var file = new StreamWriter(@"LoggingDirectory\AnotherLogFile.log"))
		{
			file.WriteLine("Some stuff written to a different log file in the logging directory");
		}
	}

	[Test]
	[ExclusivelyUses("LoggingDirectory")]
	public void TestThatCleansUpTheLoggingDirectory()
	{
		if (Directory.Exists("LoggingDirectory"))
			Directory.Delete("LoggingDirectory", true);
	}
}


In the above example, there are two tests that write to different files within a logging directory. These tests can safely be run together (concurrently) in different processes, as they touch different files and don't step on each other's resources. However, the third test cannot safely be run concurrently with either of the other tests, as this test makes changes to the file system by deleting a directory required by the other tests to run.

So it can be said that the two tests writing to log files are making 'Inclusive' use of the logging directory, where the third test uses it 'exclusively'. When marked as such, NCrunch will avoid running the third test if either of the other tests is currently executing.

I hope this makes sense!

Cheers,

Remco
1 user thanked Remco for this useful post.
hacker_112 on 9/4/2013(UTC)
hacker_112
#3 Posted : Wednesday, September 4, 2013 7:35:31 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/4/2013(UTC)
Posts: 2

Thanks: 1 times
Now it makes totally sense! Thanks so much for a good explanation and good code example :)

Cheers,
David
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.042 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download