Archive for the 'snippets' Category

Published by breki on 18 Oct 2010

Web Testing & Gallio: A Little Helpful Trick

When doing automatic testing of Web apps using unit testing frameworks, it can be a pain in the butt to pinpoint the proper HTML element. A lot of times tests will fail because you used a wrong locator, but since the browser will automatically close after the test, you don’t have an access to the HTML code of the page to look at what’s actually there.

Fortunately Gallio provides a class called TestContext which contains the current information about the running test and which you can use to determine if the latest test is successful or not. This can then be used to run your custom handling code during the test teardown:

        [TearDown]
        protected virtual void Teardown()
        {
            if (TestContext.CurrentContext.Outcome.Status == TestStatus.Failed)
            {
                using (TestLog.BeginSection("Failed web page HTML"))
                    TestLog.Write(WebDriver.PageSource);
            }
        }

In the above snippet, we record the current Web page’s HTML code into Gallio’s log (the TestLog class). To avoid spamming the log, we do this for failed tests only.

Gallio provides a powerful framework which I think is very much underused, mostly because the documentation is not very detailed (to say the least).

Published by breki on 27 Oct 2009

C#: Service Caching Proxies With The A Little Help From Some Functional Code

On a current project we’re working on in our company, we are developing a web application which accesses the back-end through some web services. Nothing special really, except that certain web services provide pretty static information like lookup tables, which don’t change very often, so it’s not really necessary to refetch them all the time.

Since we are heavily relying on the dependency injection and all the back-end services are exposed through C# interfaces, it wasn’t really hard to develop a new implementation of such an interface which calls the back-end service and then caches the results in the HTTP cache to be used by any subsequent calls.

Here’s a simple example of a service interface:

public interface IBackendService
{
    List<string> ListSomeStrings(string parameter1, int parameter2);
}

I won’t bore you with an implementation of this, since it’s really a dumb one. What’s interesting is how the cached implementation looks like:

public class BackendServiceCachingProxy : CachingProxyBase<IBackendService>, IBackendService
{
    public BackendServiceCachingProxy(
        IBackendService wrappedService, 
        Cache cache,
        TimeSpan slidingCacheExpiration)
        : base(wrappedService, cache, slidingCacheExpiration)
    {
    }

    public List<string> ListSomeStrings(string parameter1, int parameter2)
    {
        return CallServiceMethod<string>(
            () => ConstructCacheKey("SomeStrings", parameter1, parameter2),
            s => s.ListSomeStrings(parameter1, parameter2));
    }
}

Our caching wrapper inherits from a base class CachingProxyBase. Before showing you the code for CachingProxyBase, let me just quickly explain what the BackendServiceCachingProxy code does.

We’re using HTTP cache, so we have to supply it to the class in the constructor. We also specify how long the cache should be valid (slidingCacheExpiration parameter).

Each service method in the caching wrapper now uses CallServiceMethod method to implement the cached service method calls. You specify two parameters:

  1. A function delegate for constructing the cache key. This key should be unique for each unique combination of method’s input parameters. The CachingProxyBase offers a helper method ConstructCacheKey to help you with this task.
  2. A function delegate for calling the method on the service. This delegate will be used for calling the actual service implementation which will contact the back-end.

 

CachingProxyBase

And now the implementation of the CachingProxyBase:

public abstract class CachingProxyBase<TService>
{
    public void ExpireAllCachedData()
    {
        foreach (System.Collections.DictionaryEntry entry in cache)
            cache.Remove(entry.Key.ToString());
    }

    protected CachingProxyBase(
        TService wrappedService,
        Cache cache,
        TimeSpan slidingCacheExpiration)
    {
        this.wrappedService = wrappedService;
        this.cache = cache;
        this.slidingCacheExpiration = slidingCacheExpiration;
    }

    protected Cache Cache
    {
        get { return cache; }
    }

    protected TimeSpan SlidingCacheExpiration
    {
        get { return slidingCacheExpiration; }
    }

    protected TService WrappedService
    {
        get { return wrappedService; }
    }

    protected TValue CallServiceMethod<TValue>(
        Func<string> constructCacheKeyFunc,
        Func<TService, TValue> serviceMethodFunc)
        where TValue : class
    {
        string cacheKey = constructCacheKeyFunc();

        TValue cachedValue = GetCachedValue<TValue>(cacheKey);

        if (cachedValue == null)
        {
            cachedValue = serviceMethodFunc(WrappedService);
            CacheValue(cacheKey, cachedValue);
        }

        return cachedValue;
    }

    protected string ConstructCacheKey(
        string function, 
        string environmentId, 
        params object[] args)
    {
        StringBuilder cacheKeyBuilder = new StringBuilder();
        cacheKeyBuilder.AppendFormat(
            CultureInfo.InvariantCulture,
            "Proxy-{0}-{1}",
            function,
            environmentId);

        foreach (object arg in args)
            cacheKeyBuilder.AppendFormat(CultureInfo.InvariantCulture, "-{0}", arg);

        return cacheKeyBuilder.ToString();
    }

    private void CacheValue(string cacheKey, object value)
    {
        Cache.Insert(
            cacheKey,
            value,
            null,
            Cache.NoAbsoluteExpiration,
            SlidingCacheExpiration);
    }

    [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
    private T GetCachedValue<T>(string cacheKey) 
    {
        return (T)cache.Get(cacheKey);
    }

    private readonly TService wrappedService;
    private readonly Cache cache;
    private readonly TimeSpan slidingCacheExpiration;
}

TService is a generic parameter representing the service interface the proxy is enhancing with the caching. I think the code is pretty self-explanatory. The ExpireAllCachedData method is exposed to be used in unit tests to make sure the cache is empty before doing any work.

Using It With Windsor Castle

I’ve made a helper method for configuring in Windsor Castle a specific back-end service with or without caching:

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
protected void RegisterServiceWithOptionalCache<TService, TImplementation, TCachedImplementation>(
    IWindsorContainer container,
    TimeSpan slidingCacheExpiration)
    where TImplementation : TService
    where TCachedImplementation : TService
{
    string serviceName = typeof(TService).Name;

    if (IsCachingUsed)
    {
        container.Register(
            Component.For<TService>().ImplementedBy<TCachedImplementation>()
                .ServiceOverrides(ServiceOverride.ForKey("wrappedService").Eq(serviceName))
                .Parameters(Parameter.ForKey("slidingCacheExpiration").Eq(slidingCacheExpiration.ToString())));

        if (log.IsDebugEnabled)
            log.DebugFormat("Registered {0}", typeof(TCachedImplementation).Name);
    }

    container.Register(
        Component.For<TService>().ImplementedBy<TImplementation>()
            .Named(serviceName));            
}

 

Conclusion

There are probably more elegant ways of doing this (probably using Windsor’s interceptors and reflection), but I think this code does its job well if you have a small number of service methods to cover.

Published by breki on 27 Jan 2009

SharpZipLib: Making It Work For Linux/Mac

Mr. Monster Mouth
Creative Commons License photo credit: massdistraction

I’m writing this post since I’ve found a lot of people having similar problems, so hopefully it will be useful to someone.

I’ve had Linux/Mac people reporting that the ZIP package of GroundTruth which I released yesterday could not be extracted. The package was created programmatically using the SharpZibLib, an open source .NET ZIP library. I myself did not detect any problems, it works OK on my Vista machines.

I’ve done some investigation (Google, inspecting other open source projects…) and found a small hint what could be the culprit – I wasn’t setting the ZipEntry.Size property when I was adding files to the package. So here I’m posting a code snippet on how to create a ZIP package with selectively added files:

using (FileStream zipFileStream = new FileStream(
    zipFileName,
    FileMode.Create,
    FileAccess.ReadWrite,
    FileShare.None))
{
    using (ZipOutputStream zipStream = new ZipOutputStream(zipFileStream))
    {
        byte[] buffer = new byte[1024 * 1024];

        foreach (string fileName in filesToZip)
        {
            string cleanedFileName = ZipEntry.CleanName(fileName);

            using (FileStream fileStream = File.OpenRead (fileName))
            {
                ZipEntry entry = new ZipEntry (cleanedFileName);
                entry.DateTime = File.GetLastWriteTime(fileName);
                entry.Size = fileStream.Length;
                zipStream.PutNextEntry (entry);

                int sourceBytes;

                while (true)
                {
                    sourceBytes = fileStream.Read(buffer, 0, buffer.Length);

                    if (sourceBytes == 0)
                        break;

                    zipStream.Write(buffer, 0, sourceBytes);
                }
            }
        }
    }
}
 

Published by breki on 09 Jan 2009

MbUnit: Inconclusive Test Results

Questions
Creative Commons License photo credit: Oberazzi

UPDATE: Jeff Brown pointed me to a better way of doing some of the things discussed in this post, so I’ve updated the post.

One of the lesser known (and documented) features of MbUnit and Gallio is marking tests as inconclusive:

       [Test]
        public void InconclusiveTest()
        {
            if (WeDeterminedTheTestCannotBeRun)
               Assert.Inconclusive("Inconclusive message");

            WeThrowAnExceptionButItDoesNotMatter()
        }

By calling Assert.Inconclusive() we tell the test run it should mark this test case as inconclusive. Assert.Inconclusive() does not throw any exceptions, the tests continues to run, but even if the later code throws an exception or some assert fails, the test outcome will still be marked as inconclusive:

      456 run, 455 passed, 0 failed, 1 inconclusive, 1 skipped (1 ignored)

The build will not fail if we have one or more inconclusive tests. How does this come in handy? Sometimes you have tests which access certain external resources like internet pages. You want to be able to run such tests without causing the build to fail if internet connection is temporarily not available (I’m not saying that this is a good pattern for writing tests, just giving an example). One way to do this would be to first check the internet connection and mark the test as inconclusive if the connection is not available.

The second scenario (which we actually use in our acceptance test framework) is when you rely on certain test facility methods in order to execute the tests. Since these methods are often implemented in parallel with actual test code, we want to be able to mark them as not available until they are finished. We do this again by invoking Assert.Inconclusive() inside such methods, which will cause all test code that use these methods to have an inconclusive test result.

An alternative would be throwing NotImplementedExceptions, but we want to separate tests which actually failed from those which are not fully implemented.

There is a better way: Gallio: Setting Test Outcome Anyway You Like

Published by breki on 08 Jan 2009

Asserting Than An Assertion Has Failed

Opie thought he would find a kewl looking girl cat on the computer
Creative Commons License photo credit: turtlemom4bacon

UPDATE: Jeff Brown, one of the architects of MbUnit and Gallio, responded to my post suggesting other (=better) ways of checking assertions. I’ve added his suggestions at the bottom of the post.

We’re developing an acceptance test framework (which I will write more about when it reaches some maturity state) which will use MbUnit and Gallio to execute the test code. We developed some utility assertions of our own (which in turn use MbUnit Assert* methods) and we wanted to test them using MbUnit. So basically we wanted to unit test the unit test code ;).

An interesting problem occurred when we wanted to test that one of these assertions actually fails under certain conditions. MbUnit throws AssertionException when an assertion fails, but this gets eaten by test runners as an indicator that the test case has failed (obviously). Of course, we didn’t want the test to fail, since we expect our assertion method to fail… OK I know it sound complicated, so let me show you the code instead of blabbering too much:

try
{
    runner.AssertSmsReceived("incorrect sms");
    Assert.Fail("Exception should have been thrown here");
}
catch (AssertionException ex)
{
    // this is to filter out an assertion for wrong SMS received.
    Assert.IsFalse(ex.Message.Contains("Exception should have been thrown here"));
}

Explanation: we expect runner.AssertSmsReceived() to throw AssertException. That’s why we catch this exception afterwards. But if the method has not failed, we force the failure with Assert.Fail(). Since both conditions throw the same type of an exception (AssertionException), we check its message contents to find out if the proper condition was met.

There’s probably a better way to do this, but I haven’t found it (other than throwing a different type of exception instead of calling Assert.Fail(), but I wanted to avoid this because Assert.Fail() gives a cleaner test result output). Or it’s just too late in the day for me to think…

Yes, there’s a better way (thanks Jeff):

   // check if received message is correct
    AssertionFailure[] failures = AssertionHelper.Eval(()
         => runner.AssertSmsReceived("incorrect sms"));
    Assert.AreEqual(1, failures.Length);
    Assert.IsTrue(failures[0].Message.Contains("did not receive an expected SMS message"));

Jeff also posted a helper class which Gallio guys use for testing MbUnit v3 asserts:

[TestFrameworkInternal]
public static AssertionFailure[] Capture(Gallio.Action action)
{
    AssertionFailure[] failures = AssertionHelper.Eval(action);

    if (failures.Length != 0)
    {
        using (TestLog.BeginSection(&#8221;Captured Assertion Failures&#8221;))
        {
            foreach (AssertionFailure failure in failures)
                failure.WriteTo(TestLog.Default);
        }
    }

    return failures;
}

Published by breki on 05 Jan 2009

Visiting Regex.Replace() Method And MatchEvaluator Delegate

In his post about creating string formatting function which would use named parameters instead of numbered ones like {0}, {1} etc, Phil Haack mentions perhaps little known, but very useful method of the Regex class: Replace() method, specifically the override which uses MatchEvaluator delegate.

I first used this method few days ago when I was implementing labeling of map elements for GroundTruth, a new mapmaking tool for Garmin which I’m currently working on. The idea was to provide something like a format string which would tell the tool how to construct a label for map elements (roads, cities etc). One example would be to generate map label of a mountain peak with an attached elevation: “Mount Everest, 8848 M“.

The solution was to provide special functions (or properties) which would expand to actual values of map element’s tags, but by also taking account of the context (for example, do we show elevation in meters or feet?). These functions would be encoded in the string in form of $FunctionName. This is where Regex.Replace() comes in handy:

string expandedString = regexFunction.Replace(
    format,
    match =>
{
    string functionName = match.Groups["function"].Value;

    if (false == registeredFunctions.ContainsKey(functionName))
        throw new ArgumentException(
            String.Format(
                CultureInfo.InvariantCulture,
                "Unknown label building function ‘{0}’",
                functionName));

    ILabelBuildingFunction function = registeredFunctions[functionName];
    string result = function.Calculate(mapMakerSettings, osmObject, osmTag);

    return result;
});

So what are we doing here? We are telling Regex to give us each Regex match and we will then return a replacement string for it. The anonymous delegate extracts the name of the function from this Match and looks it up in the dictionary of supported functions. Since each function has a corresponding object, we call on this object to give us an actual value of the function (Calculate() method). We then return this value to Regex as a replacement.

The regexFunction is defined as a static class member:

private static readonly Regex regexFunction = new Regex (@"\$(?<function>\w+)",
    RegexOptions.Compiled | RegexOptions.ExplicitCapture);

Creating Regex objects is expensive, so it’s better to instantiate them once as static members. Using RegexOptions.Compiled is also recommended, as is reading Jeff Atwood’s post about it.

Published by breki on 04 Feb 2008

C# snippet for the Disposable pattern

This is the first post in a series of posts in which I will publish some C# code snippets I find useful in everyday C# coding.

This snippets inserts all of the necessary code to implement Disposable pattern in a C# class. The code is basically the example of how to implement IDisposable interface taken from the FxCop help pages, so this snippet resolves that FxCop issue.

Note the two “TODO”-s where you can add your code for disposing of managed and unmanaged resources.


<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <codesnippet format="1.0.0">

            <description>Implements a disposable pattern for the class.</description>
            <author>Igor Brejc</author>
            <keywords>
                <keyword>IDisposable</keyword>
                <keyword>Dispose</keyword>
            </keywords>
            <snippettypes>
                <snippettype>Expansion</snippettype>
            </snippettypes>

        <snippet>
            <code language="CSharp" kind="method decl">
                <!–[CDATA[
      #region IDisposable Members

      /// <summary>


      /// Performs application-defined tasks associated with freeing, releasing, or
      /// resetting unmanaged resources.
      /// </summary>
      public void Dispose()
      {
         Dispose(true);
         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// Disposes the object.
      /// </summary>
      ///
<div  name="disposing"></div>
If <code>false</code>, cleans up native resources.
      /// If <code>true</code> cleans up both managed and native resources</param>
      protected virtual void Dispose(bool disposing)
      {
            if (false == disposed)
            {
                // TODO: clean native resources        

             if (disposing)
             {
                    // TODO: clean managed resources
                    $end$
             }

             disposed = true;
            }
      }

      private bool disposed;

      #endregion
                ]]–>
            </code>
        </snippet>
    </codesnippet>
</codesnippets>