[dba-VB] C# programming exercises: Sample 1 - Run parallel tests from this assembly test classes

Salakhetdinov Shamil mcp2004 at mail.ru
Tue Jun 19 11:27:36 CDT 2012


Hi Gustav --

Yes, I have run this code in LINQPad 4.42.1, I have got purchased a Premium version to have Intellisense etc. 
LINQPad is a great C# learning/mastering/"code snippeting"/experimenting tool. 
But you know that LINQPad is a great tool AFAIK :)

I have put .Sleep(1000) in Test1 class Setup method to show that in this case Test2 runs first and in parallel - did you mean that?

> That looks clever and tight.
Thank you, just a bit maybe - when I'm reading "C# 4.0 in a Nutshell" by Joseph and Ben Albahari I'm feeling myself as a 1st grade pupil - the concepts and techniques of modern C# (functional)  parallel programming they present in many parts of heir book are so advanced...

BTW, I still can't get how LINQPad dynamically builds ADO.NET EF model/assembly from given MS SQL database connection - have you seen/read anywhere how it's done, what (third party) .NET classlibs they use?

Thank you.

-- Shamil

Tue, 19 Jun 2012 18:18:13 +0200 от "Gustav Brock" <gustav at cactus.dk>:
> Hi Shamil
> 
> That looks clever and tight.
> I guess if you didn't have the Sleep(1000) method, the code would run with one timestamp only.
> 
> But do you really run this from within LINQPad?
> 
> /gustav
> 
> 
> >>> Salakhetdinov Shamil <mcp2004 at mail.ru> 19-06-12 17:57 >>>
> Hi All -
> 
> I wanted to share some code snippets I'm getting written here while mastering C# programming using LINQPad.
> Presented here code snippet will instantiate and run in parallel two sample test classes resulting in an output as the following:
> 
> 19.06.2012 19:49:42: Test1 instantiated.
> 19.06.2012 19:49:42: Test2 instantiated.
> 19.06.2012 19:49:42: Test1 setup.
> 19.06.2012 19:49:42: Test2 setup.
> 19.06.2012 19:49:43: Test2 run started...
> 19.06.2012 19:49:44: Test1 run started...
> 19.06.2012 19:49:44: Test2 run completed.
> 19.06.2012 19:49:44: Test2 tear down.
> 19.06.2012 19:49:45: Test1 run completed.
> 19.06.2012 19:49:45: Test1 tear down.
> 
> All and any comments & remarks are very welcome!
> 
> Thank you.
> 
> -- Shamil
> 
> P.S. Code snippet:
> 
> --- cut here ---
> 
> // LINQPad test program start code line - to be commented under VS
> static void Main() { Program.Main(); }
> 
> // Program - main entry method
> class Program
> {
> 	public static void Main(string[] args = null)
> 	{
> 		try
> 		{
> 		    // Short def: 
> 			// Run tests from this assembly test classes in parallel
> 			//
> 			// Long def:
> 			// Select classes in this assembly,
> 			// which type name starts with a string 'Test',
> 			// order selected type names in ascending order,
> 			// and then for each selected class
> 			// start parallel thread, which will
> 			// create class instance,
> 			// will cast that instance to ITest interface,
> 			// and if cast returns not null ITest instance
> 			// will call Setup(), Run() and TearDown() methods...
> 			typeof(Program).Assembly.GetTypes()
> 				.Where(x => x.Name.StartsWith("Test"))
> 				.OrderBy(x => x.Name) 
> 				.AsParallel() 
> 				.ForAll(x => 
> 					{
> 					   ITest test = Activator.CreateInstance(x) as ITest;
> 					   if (test != null) 
> 					   {
> 						 System.Console.WriteLine("{0}: {1} instantiated.", DateTime.Now, test.GetType().Name);
> 					         test.Setup(); 
> 					   	 test.Run();
> 						 test.TearDown(); 
> 					   }
> 					});
> 		}
> 		catch (Exception ex)
> 		{
> 			System.Console.WriteLine("Error = '{0}'", ex.Message);   
> 		}
> 	}
> }
> 
> // Logger - utility class
> public class Logger
> {
> 	protected void log(params dynamic[] args)
> 	{
> 		System.Console.WriteLine(args[0], args[1], args[2].Name);
> 	}
> }
> 
> // ITest - test setup, run and tear down interface
> public interface ITest
> {
> 	void Setup();
> 	void Run();
> 	void TearDown();
> }
> 
> // Test2 - sample test class
> public class Test2: Logger, ITest
> {
> 	public void Setup() 
> 	{
> 	 	log("{0}: {1} setup.", DateTime.Now, typeof(Test2));
> 	} 
> 	public void TearDown() 
> 	{
> 		log("{0}: {1} tear down.", DateTime.Now, typeof(Test2));
> 	}
> 	void ITest.Run()
> 	{
> 		log("{0}: {1} run started...", DateTime.Now, this.GetType());
> 		System.Threading.Thread.Sleep(1000);
> 		log("{0}: {1} run completed.", DateTime.Now, this.GetType());
> 	}
> }
> 
> // Test2 - sample test class
> public class Test1: Logger, ITest
> {
> 	public void Setup() 
> 	{
> 		log("{0}: {1} setup.", DateTime.Now, this.GetType());
> 		System.Threading.Thread.Sleep(1000);
> 	} 
> 	public void TearDown() 
> 	{
> 		log("{0}: {1} tear down.", DateTime.Now, this.GetType());
> 	}
> 	void ITest.Run()
> 	{
> 		log("{0}: {1} run started...", DateTime.Now,  this.GetType());
> 		System.Threading.Thread.Sleep(1000);
> 		log("{0}: {1} run completed.", DateTime.Now, this.GetType());
> 	}
> }
> 
> -- cut here --
> 
> Thank you.
> 
> -- Shamil
> 
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
> 
> 



More information about the dba-VB mailing list