Gustav Brock
gustav at cactus.dk
Tue Jun 19 11:18:13 CDT 2012
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