Skip to content

Commit

Permalink
added a new unit test TestCommandWithDependencyInjection
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 31, 2024
1 parent acb215a commit 1f17983
Showing 1 changed file with 107 additions and 1 deletion.
108 changes: 107 additions & 1 deletion test/SuperSocket.Tests/CommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public async Task TestCommands(Type hostConfiguratorType)
// register all commands in one assembly
//commandOptions.AddCommandAssembly(typeof(SUB).GetTypeInfo().Assembly);
}).BuildAsServer())
})
.BuildAsServer())
{

Assert.Equal("TestServer", server.Name);
Expand Down Expand Up @@ -423,5 +424,110 @@ public async Task TestCommandFilter(Type hostConfiguratorType)
await server.StopAsync();
}
}

[Theory]
[InlineData(typeof(RegularHostConfigurator))]
[InlineData(typeof(SecureHostConfigurator))]
public async Task TestCommandWithDependencyInjection(Type hostConfiguratorType)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
using (var server = CreateSocketServerBuilder<StringPackageInfo, CommandLinePipelineFilter>(hostConfigurator)
.UseCommand(commandOptions =>
{
commandOptions.AddCommand<DOUBLE>();
})
.ConfigureServices((context, services) =>
{
services.AddSingleton<DataStore>();
})
.BuildAsServer())
{

Assert.Equal("TestServer", server.Name);

Assert.True(await server.StartAsync());
OutputHelper.WriteLine("Server started.");

var dataStore = server.ServiceProvider.GetService<DataStore>();

// Default value of data store
Assert.Equal(1, dataStore.Value);

var client = hostConfigurator.CreateClient();

using (var stream = await hostConfigurator.GetClientStream(client))
using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4))
{
dataStore.ResetTask();
await streamWriter.WriteAsync("DOUBLE\r\n");
await streamWriter.FlushAsync();

var newValue = await dataStore.WaitNewValue();

Assert.Equal(2, newValue);

dataStore.ResetTask();
await streamWriter.WriteAsync("DOUBLE\r\n");
await streamWriter.FlushAsync();

newValue = await dataStore.WaitNewValue();

Assert.Equal(4, newValue);

dataStore.ResetTask();
await streamWriter.WriteAsync("DOUBLE\r\n");
await streamWriter.FlushAsync();

newValue = await dataStore.WaitNewValue();

Assert.Equal(8, newValue);
}

await server.StopAsync();
}
}

public class DataStore
{
private int _value = 1;

public int Value
{
get { return _value; }
set
{
_value = value;
_newValueTaskSource?.SetResult(value);
}
}

private TaskCompletionSource<int> _newValueTaskSource;

public Task<int> WaitNewValue()
{
return _newValueTaskSource.Task;
}

public void ResetTask()
{
_newValueTaskSource = new TaskCompletionSource<int>();
}
}

public class DOUBLE : IAsyncCommand<StringPackageInfo>
{
private readonly DataStore _dataStore;

public DOUBLE(DataStore dataStore)
{
_dataStore = dataStore;
}

public ValueTask ExecuteAsync(IAppSession session, StringPackageInfo package, CancellationToken cancellationToken)
{
_dataStore.Value = _dataStore.Value * 2;
return ValueTask.CompletedTask;
}
}
}
}

0 comments on commit 1f17983

Please sign in to comment.