Skip to content

Instantly share code, notes, and snippets.

@kohbo
Created April 4, 2025 15:24
Show Gist options
  • Save kohbo/a5cb8b6146c6eacdd6054367168ac9fe to your computer and use it in GitHub Desktop.
Save kohbo/a5cb8b6146c6eacdd6054367168ac9fe to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace YourNamespace.Redis
{
/// <summary>
/// Interface for Redis queue operations
/// </summary>
public interface IRedisQueue<T>
{
Task<bool> EnqueueAsync(T item);
Task<T> DequeueAsync();
Task<T> PeekAsync();
Task<int> GetLengthAsync();
Task<bool> IsEmptyAsync();
Task ClearAsync();
}
/// <summary>
/// In-memory stub implementation of a Redis queue for integration testing
/// </summary>
public class RedisQueueStub<T> : IRedisQueue<T>
{
private readonly Queue<T> _inMemoryQueue = new Queue<T>();
private readonly object _syncLock = new object();
public Task<bool> EnqueueAsync(T item)
{
if (item == null)
return Task.FromResult(false);
lock (_syncLock)
{
_inMemoryQueue.Enqueue(item);
}
return Task.FromResult(true);
}
public Task<T> DequeueAsync()
{
lock (_syncLock)
{
if (_inMemoryQueue.Count == 0)
return Task.FromResult<T>(default);
return Task.FromResult(_inMemoryQueue.Dequeue());
}
}
public Task<T> PeekAsync()
{
lock (_syncLock)
{
if (_inMemoryQueue.Count == 0)
return Task.FromResult<T>(default);
return Task.FromResult(_inMemoryQueue.Peek());
}
}
public Task<int> GetLengthAsync()
{
lock (_syncLock)
{
return Task.FromResult(_inMemoryQueue.Count);
}
}
public Task<bool> IsEmptyAsync()
{
lock (_syncLock)
{
return Task.FromResult(_inMemoryQueue.Count == 0);
}
}
public Task ClearAsync()
{
lock (_syncLock)
{
_inMemoryQueue.Clear();
}
return Task.CompletedTask;
}
}
/// <summary>
/// Factory for creating Redis queue instances
/// </summary>
public static class RedisQueueFactory
{
public static IRedisQueue<T> CreateStub<T>()
{
return new RedisQueueStub<T>();
}
// For real implementation (commented out for stub)
/*
public static IRedisQueue<T> Create<T>(string connectionString)
{
// Return actual Redis implementation
throw new NotImplementedException("Real Redis implementation not included in stub");
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment