简体   繁体   中英

Azure Redis Cache: is it possible to connect to the cache in an integration test?

I am running integration tests against an Azure Redis Cache. This is my very straightforward cache implementation:

public class RedisCacheEngine : ICacheEngine
{
    private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        var config = new ConfigurationService();
        var connectionString = config.Get("Redis.ConnectionString");
        var connection = ConnectionMultiplexer.Connect(connectionString);
        return connection;
    });

    private static ConnectionMultiplexer Connection => LazyConnection.Value;

    public TValue Get<TValue>(string key) where TValue : class
    {
        var redisValue = Connection.GetDatabase().StringGet(key);
        return redisValue == RedisValue.Null 
            ? default(TValue) 
            : JsonConvert.DeserializeObject<TValue>(redisValue);
    }

    public void Set<TValue>(string key, TValue value) where TValue : class => 
        Connection.GetDatabase().StringSet(key, JsonConvert.SerializeObject(value));

    public void Remove(string key) => Connection.GetDatabase().KeyDelete(key);
}

When I look at the connection object in the debugger, its failureMessage field reads "SocketFailure on PING". I don't believe the server is timing out, as the timeout window is a generous five seconds and I'm on a fast system, with a fast connection.

The connection string is a standard Azure Redis Cache string, of the form

myapp.redis.cache.windows.net:6380,password=___,ssl=True,abortConnect=False

I have tried setting ssl = False, but without success.

I would ideally like to be able to run these tests on my build environment, but at the moment I can't retrieve a working connection. I can't see anything obvious that I may be missing in the documemtation. Are there any checks I can run to ensure I'm doing the right thing?

is it possible to connect to the cache in an integration test?

Yes, I do a demo with your mentioned code just using connectionString directly, it works correctly on my side.

 private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            var connectionString = "mytest.redis.cache.windows.net:6380,password=xxxxxxxx,ssl=True,abortConnect=False";
            var connection = ConnectionMultiplexer.Connect(connectionString);
            return connection;
        });

According to exception message SocketFailure on PING , I assume that your development environment firewall which is blocking port 6379, 6380 .

If your redis service is Premium pricing tier , please have a check whether there is a firewall rule that blocks the client connection.

If your network is behind a firewall, please have a try to make sure the ports are open. Or please have a try to use another network environment.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM