Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchmidt committed Aug 1, 2024
1 parent dce28e3 commit 32ca86e
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 279 deletions.
5 changes: 4 additions & 1 deletion src/ProjectOrigin.Chronicler.Server/DatabaseScripts/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ CREATE TABLE read_blocks (
CREATE OR REPLACE FUNCTION insert_read_block()
RETURNS TRIGGER AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM read_blocks WHERE registry_name = NEW.registry_name) THEN
PERFORM 1 FROM read_blocks WHERE registry_name = NEW.registry_name;

IF NOT FOUND THEN
INSERT INTO read_blocks (registry_name, block_height, read_at)
VALUES (NEW.registry_name, -1, NOW());
END IF;

RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ProjectOrigin.Chronicler.Server.Exceptions
{
public class RegistryNotKnownException : Exception
{
public RegistryNotKnownException(string registryName)
: base($"Registry {registryName} is not known")
{
}
}
}
7 changes: 4 additions & 3 deletions src/ProjectOrigin.Chronicler.Server/RegistryService.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Microsoft.Extensions.Options;
using ProjectOrigin.Chronicler.Server.Exceptions;
using ProjectOrigin.Chronicler.Server.Options;
using static ProjectOrigin.Registry.V1.RegistryService;

namespace ProjectOrigin.Chronicler.Server;

public class RegistryService : IRegistryService
{
ConcurrentDictionary<string, GrpcChannel> _registries = new ConcurrentDictionary<string, GrpcChannel>();
private readonly ConcurrentDictionary<string, GrpcChannel> _registries = new ConcurrentDictionary<string, GrpcChannel>();
private readonly IOptionsMonitor<NetworkOptions> _optionsMonitor;

public RegistryService(IOptionsMonitor<NetworkOptions> optionsMonitor)
Expand All @@ -38,7 +40,6 @@ public RegistryService(IOptionsMonitor<NetworkOptions> optionsMonitor)
}
}
});

}

private RegistryServiceClient CreateClient(string registryName)
Expand All @@ -48,7 +49,7 @@ private RegistryServiceClient CreateClient(string registryName)
(name) =>
{
if (!_optionsMonitor.CurrentValue.RegistryUrls.TryGetValue(name, out var address))
throw new System.Exception($"Registry {name} not found in configuration");
throw new RegistryNotKnownException($"Registry {name} not found in configuration");

return GrpcChannel.ForAddress(address, new GrpcChannelOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private async Task<TOption> LoadFromHttp(CancellationToken stoppingToken)
var response = await httpClient.GetAsync(_originOptions.ConfigurationUri, stoppingToken);
response.EnsureSuccessStatusCode();

return await response.Content.ReadFromJsonAsync<TOption>()
return await response.Content.ReadFromJsonAsync<TOption>(stoppingToken)
?? throw new JsonException("Failed to read options from response.");
}

Expand Down
Loading

0 comments on commit 32ca86e

Please sign in to comment.