Stephan G
Stephan G

Reputation: 3467

Why is azure storage queue access so slow when unit testing?

I ran into some difficulty trying to use unit testing with live Azure Storage Queues, and kept writing simpler and simpler examples to try and isolate the problem. In a nutshell, here is what seems to be happening:

So.... does anyone have a theory of what is going on here? I am baffled.

Code follows below:

Console App:

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(QueueTest("my-console-queue", "Console Test"));
    }

    public static string QueueTest(string queueName, string message)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        CloudQueue queue = queueClient.GetQueueReference(queueName);

        DateTime beforeTime = DateTime.Now;
        bool doesExist = queue.Exists();
        DateTime afterTime = DateTime.Now;
        TimeSpan ts = afterTime - beforeTime;

        if (!doesExist)
        {
            queue.Create();
        }

        CloudQueueMessage qAddMessage = new CloudQueueMessage(message);

        queue.AddMessage(qAddMessage);

        CloudQueueMessage qGetmessage = queue.GetMessage();

        string response = String.Format("{0} ({1} seconds)", qGetmessage.AsString, ts.TotalSeconds);

        return response;
    }
}

MVC App (Home Controller):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return Content(Program.QueueTest("my-mvc-queue", "Mvc Test"));
    }
}

Unit Test Method: (Note, currently expected to fail!)

[TestClass]
public class QueueUnitTests
{
    [TestMethod]
    public void CanWriteToAndReadFromQueue()
    {
        //Arrange
        string qName = "my-unit-queue";
        string message = "test message";

        //Act
        string result = Program.QueueTest(qName, message);

        //Assert
        Assert.IsTrue(String.CompareOrdinal(result,message)==0);
    }
}

Of course insight is greatly appreciated.

Upvotes: 0

Views: 590

Answers (1)

kwill
kwill

Reputation: 10998

I suspect this has nothing to do with Azure queues, but rather with .NET trying to determine your proxy settings. What happens if you make some other random System.Net call instead of the call to queue storage?

Try this line of code at the beginning of your app:

   System.Net.WebRequest.DefaultWebProxy = null;

Upvotes: 1

Related Questions