Skip to content

Commit

Permalink
Refactor LastBlockDownloader to LastBlockSource and implement singlet…
Browse files Browse the repository at this point in the history
…on pattern
  • Loading branch information
darkarki83 committed Sep 6, 2024
1 parent cb4bfe6 commit 6b2259d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/DownloaderV2/Builders/DocumentBuilder/AbstractDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json.Linq;
using DownloaderV2.Models.Covalent;

namespace DownloaderV2.Builders.DocumentBuilder;

public abstract class AbstractDocument(long savedLastBlock, long sourceLastBlock)
{
public abstract string UrlSet { get; }
public abstract string GetUrl { get; }
public long SavedLastBlock { get; set; } = savedLastBlock;
public long SourceLastBlock { get; set; } = sourceLastBlock;

public abstract Task<InputData> FetchDataAsync();

protected abstract JToken? SendRequest(string url);

protected abstract InputData DeserializeResponse(JToken responseData);
}
10 changes: 0 additions & 10 deletions src/DownloaderV2/Builders/LastBlockBuilder/LastBlockDownloader.cs

This file was deleted.

24 changes: 24 additions & 0 deletions src/DownloaderV2/Builders/LastBlockBuilder/LastBlockSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DownloaderV2.Builders.LastBlockBuilder.SourcePage;

namespace DownloaderV2.Builders.LastBlockBuilder;

public class LastBlockSource
{
private static LastBlockSource? _instance;
private static readonly object Lock = new object();
private Task<Dictionary<long, long>>? _lastBlockDictionary;
private readonly GetSourcePage _getLastBlock;

private LastBlockSource(GetSourcePage getLastBlock) => _getLastBlock = getLastBlock;

public static LastBlockSource GetInstance(GetSourcePage getLastBlock)
{
lock (Lock)
{
_instance ??= new LastBlockSource(getLastBlock);
}
return _instance;
}

public Task<Dictionary<long, long>> LastBlockDictionary => _lastBlockDictionary ??= _getLastBlock.FetchDataAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace DownloaderV2.Builders.LastBlockBuilder.SourcePage;

public class GetLastBlock(string getUri) : GetSourcePage(getUri)
public class GetLastBlock(string getUri) : GetSourcePage
{
public override string GetUri { get; } = getUri;

public GetLastBlock() : this($"{Environments.LastBlockDownloaderUrl.Get<string>()}{Environments.LastBlockKey.Get<string>()}") { }

public override async Task<Dictionary<long, long>> FetchDataAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace DownloaderV2.Builders.LastBlockBuilder.SourcePage;

public abstract class GetSourcePage(string getUri)
public abstract class GetSourcePage
{
public readonly string GetUri = getUri;
public abstract string GetUri { get; }

public abstract Task<Dictionary<long, long>> FetchDataAsync();

Expand Down
5 changes: 4 additions & 1 deletion src/DownloaderV2/DownloadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public async Task<IEnumerable<ResultObject>> HandleAsync()
{
LogRouter.LogRouter.Initialize(context.GetType());

_lastBlockDictionary = await new LastBlockDownloader(new GetLastBlock()).LastBlockDictionary;
// TODO: From ServiceProvider in the next step
var lastBlockSource = LastBlockSource.GetInstance(new GetLastBlock());
_lastBlockDictionary = await lastBlockSource.LastBlockDictionary;

var uniqueEvents = _settingDownloader.DownloaderSettings
.GroupBy(x => new { x.ChainId, x.ContractAddress, x.EventHash })
.Select(group => group.First())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using DownloaderV2.Builders.LastBlockBuilder;
using Moq;
using DownloaderV2.Builders.LastBlockBuilder;
using DownloaderV2.Builders.LastBlockBuilder.SourcePage;
using DownloaderV2.Models.LastBlock;
using Moq;
using Newtonsoft.Json;

namespace DownloaderV2.Tests.Builders.LastBlockBuilder
{
Expand All @@ -27,12 +25,31 @@ public async Task LastBlockDictionary_ReturnsCorrectData()
mockLastBlockService.Setup(service => service.FetchDataAsync())
.ReturnsAsync(expectedDictionary);

var downloader = new LastBlockDownloader(mockLastBlockService.Object);
ResetSingleton();

var downloader = LastBlockSource.GetInstance(mockLastBlockService.Object);
var actualDictionary = await downloader.LastBlockDictionary;

Assert.NotNull(actualDictionary);
Assert.Equal(expectedDictionary, actualDictionary);
}

[Fact]
public void GetInstance_ShouldReturnSameInstance()
{
var mockGetSourcePage = new Mock<GetLastBlock>("testUri");

var instance1 = LastBlockSource.GetInstance(mockGetSourcePage.Object);
var instance2 = LastBlockSource.GetInstance(mockGetSourcePage.Object);

Assert.Equal(instance1, instance2);
}

private static void ResetSingleton()
{
typeof(LastBlockSource)
.GetField("_instance", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
?.SetValue(null, null);
}
}
}

0 comments on commit 6b2259d

Please sign in to comment.