node.jsazureunit-testingazure-storage-emulatorazurite

How to check whether a Node.js service is running - in my case Azurite emulator?


I am developing a C# application that should run in Azure. I want to use the Azurite emulator to test it locally. What I want to achieve is: Have my tests detect whether Azurite is running and abort quickly with a nice error message if it is not running.

Apparently Azurite runs on Node.js.

With the old Microsoft Azure Storage Emulator, I can check it like this:

public static class AzureStorageEmulatorDetector
{
    public static bool IsRunning()
    {
        const string exePath = @"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe";
        if (!File.Exists(exePath))
            return false;
        var processStartInfo = new ProcessStartInfo {FileName = exePath, Arguments = "status", RedirectStandardOutput = true};
        var process = new Process {StartInfo = processStartInfo};
        process.Start();
        process.WaitForExit();
        var processOutput = process.StandardOutput.ReadToEnd();
        return processOutput.Contains("IsRunning: True");
    }
}

I want to accomplish something similar with Azurite.

I have installed Azurite like this:

npm install -g azurite

I run it like this:

azurite --silent --location C:\temp\Azurite --debug c:\temp\Azurite\debug.log

I notice that the Azurite command-line application has no parameter that tells me whether it is already running. And when I start Azurite from the console I don't see any process or service in Task Explorer called anything like "azurite". So I don't know what process I'm supposed to check for.

EDIT: Apparently Azurite runs on Node.js. There is indeed a process called node.exe running, but that's not a sufficient condition. Can I query my running Node.js instance and get it to tell me what it is doing?

I am on Windows.

Does anyone know?


Solution

  • Inspired by the comment by Ivan Yang and this answer I did this:

    private static bool IsAzuriteRunning()
    {
        // If Azurite is running, it will run on localhost and listen on port 10000 and/or 10001.
        IPAddress expectedIp = new(new byte[] {127, 0, 0, 1});
        var expectedPorts = new[] {10000, 10001};
    
        var activeTcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
    
        var relevantListeners = activeTcpListeners.Where(t =>
                expectedPorts.Contains(t.Port) &&
                t.Address.Equals(expectedIp))
            .ToList();
    
        return relevantListeners.Any();
    }
    

    EDIT: Alternatively, check out this thread on their GitHub for other possibilities: https://github.com/Azure/Azurite/issues/734