I’m currently building a large-scale LEGO model of the Space Shuttle, and in order to get some detailed shots of a shuttle that are hard to find on the Internet, I visited the nearby Air & Space museum in Chantilly, VA.
You can find the photos on flickr.
In C#, If you need to unit test a class that fires an event in certain circumstances (perhaps even asynchronously), you need to handle a little more than just running some code and doing the assertion. You have to make sure your unit test waits for the event to be fired. Here’s one naive way of doing it, a WRONG way:
1: private bool statsUpdated = false;
2: private ManualResetEvent statsUpdatedEvent = new ManualResetEvent(false);
5: public void CheckStats()
7: BrickDatabase db = new BrickDatabase(tempFolder, maxCacheAge);
9: statsUpdated = false;
12: db.InventoryStatsUpdated += new EventHandler(db_InventoryStatsUpdated);
20: void db_InventoryStatsUpdated(object sender, EventArgs e)
22: statsUpdated = true;
There are a number of things wrong with this:
- The class variables. More complex unit test class. Have to coordinate these variables across multiple functions.
- Since they are class variables, you will want to reuse them, but you’d better remember to reset the event and the boolean every time!
- Have to have two functions to do something really, really simple.
- The WaitOne() does not have a timeout, so if the wait is ever satisfied then statsUpdated is guaranteed to be true.
Here’s a better way of doing it, using anonymous methods in C# 2.0:
2: public void CheckStats()
4: BrickDatabase db = new BrickDatabase(tempFolder, maxCacheAge);
5: bool statsUpdated = false;
6: ManualResetEvent statsUpdatedEvent = new ManualResetEvent(false);
8: db.InventoryStatsUpdated += delegate
10: statsUpdated = true;
- The event is just part of the method. Since the event handler is an anonymous delegate, it can access the enclosing method’s local variables.
- Added 5,000ms timeout to the WaitOne() function to prevent hanging of unit tests.
Something my wife and I were talking about the other day inspired me to think about navigating the cosmos. I don’t remember our conversation, but I do remember wondering
How can we travel to a star a million light years away if the universe is constantly expanding? It wouldn’t be there once we arrived!
Which led to the general question of navigation in the universe. Even though we’re many, many decades from making these ultra-long exploratory voyages, surely someone must be thinking of these issues now so that when we do need a navigational system, it’s already available.
There is a galactic coordinate system.
(Of course, throughout all, I’m assuming that humans can be put in stasis, that we’re sending probes, robots, cyborgs, or otherwise assuming we can last the hundreds or thousands of years to get somewhere.)
But despite my best efforts at locating an answer, I find none. Has no one thought about it? We think about it with respect to the moon and Mars, but what about shifting galaxies?
Food for thought…