Skip to content

Commit

Permalink
Fix HTTP stream calls and tests (#15)
Browse files Browse the repository at this point in the history
* Fix HTTP stream calls and tests

* remove commented code

* Remove commented code
  • Loading branch information
jr-araujo authored Sep 9, 2024
1 parent 242e5ac commit 24f49ed
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Iggy_SDK/Contracts/Http/StreamResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public sealed class StreamResponse
{
public required int Id { get; init; }
public required string Name { get; init; }
public required ulong SizeBytes { get; init; }
public required ulong Size { get; init; }
public required DateTimeOffset CreatedAt { get; init; }
public required ulong MessagesCount { get; init; }
public required int TopicsCount { get; init; }
Expand Down
3 changes: 3 additions & 0 deletions Iggy_SDK/IggyClient/Implementations/HttpMessageStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ public async Task DeleteStreamAsync(Identifier streamId, CancellationToken token
public async Task<StreamResponse?> GetStreamByIdAsync(Identifier streamId, CancellationToken token = default)
{
var response = await _httpClient.GetAsync($"/streams/{streamId}", token);

if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<StreamResponse>(JsonConverterFactory.StreamResponseOptions, cancellationToken: token);
}

await HandleResponseAsync(response);

return null;
}

Expand Down
4 changes: 2 additions & 2 deletions Iggy_SDK/JsonConfiguration/StreamResponseConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class StreamResponseConverter : JsonConverter<StreamResponse>
var id = root.GetProperty(nameof(StreamResponse.Id).ToSnakeCase()).GetInt32();
var createdAt = root.GetProperty(nameof(StreamResponse.CreatedAt).ToSnakeCase()).GetUInt64();
var name = root.GetProperty(nameof(StreamResponse.Name).ToSnakeCase()).GetString();
var sizeBytesString = root.GetProperty(nameof(StreamResponse.SizeBytes).ToSnakeCase()).GetString();
var sizeBytesString = root.GetProperty(nameof(StreamResponse.Size).ToSnakeCase()).GetString();
var sizeBytesStringSplit = sizeBytesString.Split(' ');

Check warning on line 19 in Iggy_SDK/JsonConfiguration/StreamResponseConverter.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
var (sizeBytesVal, Unit) = (ulong.Parse(sizeBytesStringSplit[0]), sizeBytesStringSplit[1]);
var sizeBytes = Unit switch
Expand All @@ -42,7 +42,7 @@ public sealed class StreamResponseConverter : JsonConverter<StreamResponse>
{
Id = id,
Name = name!,
SizeBytes = sizeBytes,
Size = sizeBytes,
CreatedAt = DateTimeOffsetUtils.FromUnixTimeMicroSeconds(createdAt).LocalDateTime,
MessagesCount = messagesCount,
TopicsCount = topicsCount,
Expand Down
4 changes: 2 additions & 2 deletions Iggy_SDK/Mappers/BinaryMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ internal static StreamResponse MapStream(ReadOnlySpan<byte> payload)
Topics = topics,
CreatedAt = stream.CreatedAt,
MessagesCount = stream.MessagesCount,
SizeBytes = stream.SizeBytes
Size = stream.Size
};
}

Expand All @@ -578,7 +578,7 @@ private static (StreamResponse stream, int readBytes) MapToStream(ReadOnlySpan<b
Id = id,
TopicsCount = topicsCount,
Name = name,
SizeBytes = sizeBytes,
Size = sizeBytes,
MessagesCount = messagesCount,
CreatedAt = DateTimeOffsetUtils.FromUnixTimeMicroSeconds(createdAt).LocalDateTime
}, readBytes);
Expand Down
190 changes: 124 additions & 66 deletions Iggy_SDK_Tests/E2ETests/StreamsE2E.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,93 +17,151 @@ public StreamsE2E(IggyStreamFixture fixture)
{
_fixture = fixture;
}

[Fact(Skip = SkipMessage), TestPriority(1)]
[Fact, TestPriority(1)]
public async Task CreateStream_HappyPath_Should_CreateStream_Successfully()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x => await x.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
.Should()
.NotThrowAsync();
})).ToArray();
await Task.WhenAll(tasks);
// act & assert
await _fixture.HttpSut.Invoking(y => y.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
.Should()
.NotThrowAsync();

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x => await x.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
// .Should()
// .NotThrowAsync();
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(2)]
[Fact, TestPriority(2)]
public async Task CreateStream_Duplicate_Should_Throw_InvalidResponse()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x => await x.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
.Should()
.ThrowExactlyAsync<InvalidResponseException>();
})).ToArray();
await Task.WhenAll(tasks);
// act & assert
await _fixture.HttpSut.Invoking(y => y.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
.Should()
.ThrowExactlyAsync<InvalidResponseException>();

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x => await x.CreateStreamAsync(StreamsFixtureBootstrap.StreamRequest))
// .Should()
// .ThrowExactlyAsync<InvalidResponseException>();
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(3)]
[Fact, TestPriority(3)]
public async Task GetStreamById_Should_ReturnValidResponse()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
var response = await sut.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));
response.Should().NotBeNull();
response!.Id.Should().Be(StreamsFixtureBootstrap.StreamRequest.StreamId);
response.Name.Should().Be(StreamsFixtureBootstrap.StreamRequest.Name);
})).ToArray();
await Task.WhenAll(tasks);
// act
var response = await _fixture.HttpSut.GetStreamByIdAsync(
Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));

// assert
response.Should().NotBeNull();
response!.Id.Should().Be(StreamsFixtureBootstrap.StreamRequest.StreamId);
response.Name.Should().Be(StreamsFixtureBootstrap.StreamRequest.Name);

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// var response = await sut.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));
// response.Should().NotBeNull();
// response!.Id.Should().Be(StreamsFixtureBootstrap.StreamRequest.StreamId);
// response.Name.Should().Be(StreamsFixtureBootstrap.StreamRequest.Name);
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(4)]
[Fact, TestPriority(4)]
public async Task UpdateStream_Should_UpdateStream_Successfully()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x => await x.UpdateStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!), StreamsFixtureBootstrap.UpdateStreamRequest))
.Should()
.NotThrowAsync();
var result = await sut.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));
result.Should().NotBeNull();
result!.Name.Should().Be(StreamsFixtureBootstrap.UpdateStreamRequest.Name);
})).ToArray();
await Task.WhenAll(tasks);
// act
await _fixture.HttpSut.Invoking(y => y.UpdateStreamAsync(
Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!),
StreamsFixtureBootstrap.UpdateStreamRequest))
.Should()
.NotThrowAsync();

var result = await _fixture.HttpSut.GetStreamByIdAsync(
Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));

// assert
result.Should().NotBeNull();
result!.Name.Should().Be(StreamsFixtureBootstrap.UpdateStreamRequest.Name);

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x => await x.UpdateStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!), StreamsFixtureBootstrap.UpdateStreamRequest))
// .Should()
// .NotThrowAsync();
// var result = await sut.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!));
// result.Should().NotBeNull();
// result!.Name.Should().Be(StreamsFixtureBootstrap.UpdateStreamRequest.Name);
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(5)]
[Fact, TestPriority(5)]
public async Task DeleteStream_Should_DeleteStream_Successfully()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x => await x.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
.Should()
.NotThrowAsync();
})).ToArray();
await Task.WhenAll(tasks);
// act
await _fixture.HttpSut.Invoking(y => y
.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!))
).Should()
.NotThrowAsync();

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x => await x.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
// .Should()
// .NotThrowAsync();
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(6)]
[Fact, TestPriority(6)]
public async Task DeleteStream_Should_Throw_InvalidResponse()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x => await x.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
.Should()
.ThrowExactlyAsync<InvalidResponseException>();
})).ToArray();
await Task.WhenAll(tasks);
// act
await _fixture.HttpSut.Invoking(y => y
.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!))
).Should()
.ThrowExactlyAsync<InvalidResponseException>();

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x => await x.DeleteStreamAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
// .Should()
// .ThrowExactlyAsync<InvalidResponseException>();
// })).ToArray();
// await Task.WhenAll(tasks);
}

[Fact(Skip = SkipMessage), TestPriority(7)]
[Fact, TestPriority(7)]
public async Task GetStreamById_Should_Throw_InvalidResponse()
{
var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
{
await sut.Invoking(async x =>
await x.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
.Should()
// act
await _fixture.HttpSut.Invoking(y => y
.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!))
).Should()
.ThrowExactlyAsync<InvalidResponseException>();
})).ToArray();
await Task.WhenAll(tasks);

// TODO: This code block is commmented bacause TCP implementation is not working properly.
// var tasks = _fixture.SubjectsUnderTest.Select(sut => Task.Run(async () =>
// {
// await sut.Invoking(async x =>
// await x.GetStreamByIdAsync(Identifier.Numeric((int)StreamsFixtureBootstrap.StreamRequest.StreamId!)))
// .Should()
// .ThrowExactlyAsync<InvalidResponseException>();
// })).ToArray();
// await Task.WhenAll(tasks);
}
}
6 changes: 3 additions & 3 deletions Iggy_SDK_Tests/MapperTests/BinaryMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ public void MapStreams_ReturnsValidStreamsResponses()
var response1 = responses.ElementAt(0);
Assert.Equal(id1, response1.Id);
Assert.Equal(topicsCount1, response1.TopicsCount);
Assert.Equal(sizeBytes, response1.SizeBytes);
Assert.Equal(sizeBytes, response1.Size);
Assert.Equal(messagesCount, response1.MessagesCount);
Assert.Equal(name1, response1.Name);

var response2 = responses.ElementAt(1);
Assert.Equal(id2, response2.Id);
Assert.Equal(topicsCount2, response2.TopicsCount);
Assert.Equal(sizeBytes2, response2.SizeBytes);
Assert.Equal(sizeBytes2, response2.Size);
Assert.Equal(messagesCount2, response2.MessagesCount);
Assert.Equal(name2, response2.Name);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public void MapStream_ReturnsValidStreamResponse()
Assert.Equal(id, response.Id);
Assert.Equal(topicsCount, response.TopicsCount);
Assert.Equal(name, response.Name);
Assert.Equal(sizeBytes, response.SizeBytes);
Assert.Equal(sizeBytes, response.Size);
Assert.Equal(messagesCount, response.MessagesCount);
Assert.NotNull(response.Topics);
Assert.Single(response.Topics.ToList());
Expand Down
2 changes: 1 addition & 1 deletion Iggy_SDK_Tests/Utils/Streams/StreamFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static StreamResponse CreateStreamsResponse()
return new StreamResponse
{
Id = Random.Shared.Next(1, 10),
SizeBytes = (ulong)Random.Shared.Next(1, 10),
Size = (ulong)Random.Shared.Next(1, 10),
MessagesCount = (ulong)Random.Shared.Next(1, 10),
CreatedAt = DateTimeOffset.UtcNow,
Name = "Test Topic" + Random.Shared.Next(1, 69),
Expand Down

0 comments on commit 24f49ed

Please sign in to comment.