Merge pull request #66 from NicolasConstant/topic_add-last-sync

Topic add last sync
This commit is contained in:
Nicolas Constant 2021-01-23 06:44:07 +01:00 committed by GitHub
commit 99714330ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 682 additions and 122 deletions

View file

@ -7,5 +7,6 @@
public string AdminEmail { get; set; }
public bool ResolveMentionsInProfiles { get; set; }
public bool PublishReplies { get; set; }
public int MaxUsersCapacity { get; set; }
}
}

View file

@ -17,4 +17,8 @@
<ProjectReference Include="..\DataAccessLayers\BirdsiteLive.DAL\BirdsiteLive.DAL.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Tools\" />
</ItemGroup>
</Project>

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -45,7 +46,8 @@ namespace BirdsiteLive.Pipeline.Processors
else if (tweets.Length > 0 && user.LastTweetPostedId == -1)
{
var tweetId = tweets.Last().Id;
await _twitterUserDal.UpdateTwitterUserAsync(user.Id, tweetId, tweetId);
var now = DateTime.UtcNow;
await _twitterUserDal.UpdateTwitterUserAsync(user.Id, tweetId, tweetId, now);
}
}

View file

@ -4,9 +4,11 @@ using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using BirdsiteLive.Common.Extensions;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts;
using BirdsiteLive.Pipeline.Tools;
using Microsoft.Extensions.Logging;
namespace BirdsiteLive.Pipeline.Processors
@ -14,13 +16,16 @@ namespace BirdsiteLive.Pipeline.Processors
public class RetrieveTwitterUsersProcessor : IRetrieveTwitterUsersProcessor
{
private readonly ITwitterUserDal _twitterUserDal;
private readonly IMaxUsersNumberProvider _maxUsersNumberProvider;
private readonly ILogger<RetrieveTwitterUsersProcessor> _logger;
public int WaitFactor = 1000 * 60; //1 min
#region Ctor
public RetrieveTwitterUsersProcessor(ITwitterUserDal twitterUserDal, ILogger<RetrieveTwitterUsersProcessor> logger)
public RetrieveTwitterUsersProcessor(ITwitterUserDal twitterUserDal, IMaxUsersNumberProvider maxUsersNumberProvider, ILogger<RetrieveTwitterUsersProcessor> logger)
{
_twitterUserDal = twitterUserDal;
_maxUsersNumberProvider = maxUsersNumberProvider;
_logger = logger;
}
#endregion
@ -33,7 +38,8 @@ namespace BirdsiteLive.Pipeline.Processors
try
{
var users = await _twitterUserDal.GetAllTwitterUsersAsync();
var maxUsersNumber = await _maxUsersNumberProvider.GetMaxUsersNumberAsync();
var users = await _twitterUserDal.GetAllTwitterUsersAsync(maxUsersNumber);
var userCount = users.Any() ? users.Length : 1;
var splitNumber = (int) Math.Ceiling(userCount / 15d);

View file

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
@ -23,7 +24,8 @@ namespace BirdsiteLive.Pipeline.Processors
var userId = userWithTweetsToSync.User.Id;
var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();
var minimumSync = userWithTweetsToSync.Followers.Select(x => x.FollowingsSyncStatus[userId]).Min();
await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync);
var now = DateTime.UtcNow;
await _twitterUserDal.UpdateTwitterUserAsync(userId, lastPostedTweet, minimumSync, now);
}
}
}

View file

@ -0,0 +1,49 @@
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
namespace BirdsiteLive.Pipeline.Tools
{
public interface IMaxUsersNumberProvider
{
Task<int> GetMaxUsersNumberAsync();
}
public class MaxUsersNumberProvider : IMaxUsersNumberProvider
{
private readonly InstanceSettings _instanceSettings;
private readonly ITwitterUserDal _twitterUserDal;
private int _totalUsersCount = -1;
private int _warmUpIterations;
#region Ctor
public MaxUsersNumberProvider(InstanceSettings instanceSettings, ITwitterUserDal twitterUserDal)
{
_instanceSettings = instanceSettings;
_twitterUserDal = twitterUserDal;
}
#endregion
public async Task<int> GetMaxUsersNumberAsync()
{
// Init data
if (_totalUsersCount == -1)
{
_totalUsersCount = await _twitterUserDal.GetTwitterUsersCountAsync();
var warmUpMaxCapacity = _instanceSettings.MaxUsersCapacity / 4;
_warmUpIterations = warmUpMaxCapacity == 0 ? 0 : (int)(_totalUsersCount / (float)warmUpMaxCapacity);
}
// Return if warm up ended
if (_warmUpIterations <= 0) return _instanceSettings.MaxUsersCapacity;
// Calculate warm up value
var maxUsers = _warmUpIterations > 0
? _instanceSettings.MaxUsersCapacity / 4
: _instanceSettings.MaxUsersCapacity;
_warmUpIterations--;
return maxUsers;
}
}
}

View file

@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BirdsiteLive.Domain.Tests",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BirdsiteLive.Pipeline.Tests", "Tests\BirdsiteLive.Pipeline.Tests\BirdsiteLive.Pipeline.Tests.csproj", "{BF51CA81-5A7A-46F8-B4FB-861C6BE59298}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BirdsiteLive.DAL.Tests", "Tests\BirdsiteLive.DAL.Tests\BirdsiteLive.DAL.Tests.csproj", "{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -101,6 +103,10 @@ Global
{BF51CA81-5A7A-46F8-B4FB-861C6BE59298}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF51CA81-5A7A-46F8-B4FB-861C6BE59298}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF51CA81-5A7A-46F8-B4FB-861C6BE59298}.Release|Any CPU.Build.0 = Release|Any CPU
{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -119,6 +125,7 @@ Global
{2A8CC30D-D775-47D1-9388-F72A5C32DE2A} = {DA3C160C-4811-4E26-A5AD-42B81FAF2D7C}
{F544D745-89A8-4DEA-B61C-A7E6C53C1D63} = {A32D3458-09D0-4E0A-BA4B-8C411B816B94}
{BF51CA81-5A7A-46F8-B4FB-861C6BE59298} = {A32D3458-09D0-4E0A-BA4B-8C411B816B94}
{5A1E3EB5-6CBB-470D-8A0D-10F8C18353D5} = {A32D3458-09D0-4E0A-BA4B-8C411B816B94}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {69E8DCAD-4C37-4010-858F-5F94E6FBABCE}

View file

@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.10.1</Version>
<Version>0.11.0</Version>
</PropertyGroup>
<ItemGroup>

View file

@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.DAL;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Pipeline;
using Microsoft.Extensions.Hosting;
@ -9,36 +11,21 @@ namespace BirdsiteLive.Services
{
public class FederationService : BackgroundService
{
private readonly IDbInitializerDal _dbInitializerDal;
private readonly IDatabaseInitializer _databaseInitializer;
private readonly IStatusPublicationPipeline _statusPublicationPipeline;
#region Ctor
public FederationService(IDbInitializerDal dbInitializerDal, IStatusPublicationPipeline statusPublicationPipeline)
public FederationService(IDatabaseInitializer databaseInitializer, IStatusPublicationPipeline statusPublicationPipeline)
{
_dbInitializerDal = dbInitializerDal;
_databaseInitializer = databaseInitializer;
_statusPublicationPipeline = statusPublicationPipeline;
}
#endregion
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await DbInitAsync();
await _databaseInitializer.InitAndMigrateDbAsync();
await _statusPublicationPipeline.ExecuteAsync(stoppingToken);
}
private async Task DbInitAsync()
{
var currentVersion = await _dbInitializerDal.GetCurrentDbVersionAsync();
var mandatoryVersion = _dbInitializerDal.GetMandatoryDbVersion();
if (currentVersion == null)
{
await _dbInitializerDal.InitDbAsync();
}
else if (currentVersion != mandatoryVersion)
{
throw new NotImplementedException();
}
}
}
}

View file

@ -14,7 +14,8 @@
"Domain": "domain.name",
"AdminEmail": "me@domain.name",
"ResolveMentionsInProfiles": true,
"PublishReplies": false
"PublishReplies": false,
"MaxUsersCapacity": 1400
},
"Db": {
"Type": "postgres",

View file

@ -23,7 +23,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
public class DbInitializerPostgresDal : PostgresBase, IDbInitializerDal
{
private readonly PostgresTools _tools;
private readonly Version _currentVersion = new Version(1,0);
private readonly Version _currentVersion = new Version(2, 0);
private const string DbVersionType = "db-version";
#region Ctor
@ -32,7 +32,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
_tools = tools;
}
#endregion
public async Task<Version> GetCurrentDbVersionAsync()
{
var query = $"SELECT * FROM {_settings.DbVersionTableName} WHERE type = @type";
@ -65,17 +65,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
return _currentVersion;
}
public Tuple<Version, Version>[] GetMigrationPatterns()
{
return new Tuple<Version, Version>[0];
}
public Task MigrateDbAsync(Version from, Version to)
{
throw new NotImplementedException();
}
public async Task InitDbAsync()
public async Task<Version> InitDbAsync()
{
// Create version table
var createVersion = $@"CREATE TABLE {_settings.DbVersionTableName}
@ -124,13 +114,53 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
await _tools.ExecuteRequestAsync(createCachedTweets);
// Insert version to db
var firstVersion = new Version(1, 0);
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.ExecuteAsync(
$"INSERT INTO {_settings.DbVersionTableName} (type,major,minor) VALUES(@type,@major,@minor)",
new { type = DbVersionType, major = _currentVersion.Major, minor = _currentVersion.Minor });
new { type = DbVersionType, major = firstVersion.Major, minor = firstVersion.Minor });
}
return firstVersion;
}
public Tuple<Version, Version>[] GetMigrationPatterns()
{
return new[]
{
new Tuple<Version, Version>(new Version(1,0), new Version(2,0))
};
}
public async Task<Version> MigrateDbAsync(Version from, Version to)
{
if (from == new Version(1, 0) && to == new Version(2, 0))
{
var addLastSync = $@"ALTER TABLE {_settings.TwitterUserTableName} ADD lastSync TIMESTAMP (2) WITHOUT TIME ZONE";
await _tools.ExecuteRequestAsync(addLastSync);
var addIndex = $@"CREATE INDEX IF NOT EXISTS lastsync_twitteruser ON {_settings.TwitterUserTableName}(lastSync)";
await _tools.ExecuteRequestAsync(addIndex);
await UpdateDbVersionAsync(to);
return to;
}
throw new NotImplementedException();
}
private async Task UpdateDbVersionAsync(Version newVersion)
{
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.ExecuteAsync(
$"UPDATE {_settings.DbVersionTableName} SET major = @major, minor = @minor WHERE type = @type",
new { type = DbVersionType, major = newVersion.Major, minor = newVersion.Minor });
}
}

View file

@ -62,32 +62,33 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync()
public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync(int maxNumber)
{
var query = $"SELECT * FROM {_settings.TwitterUserTableName}";
var query = $"SELECT * FROM {_settings.TwitterUserTableName} ORDER BY lastSync ASC LIMIT @maxNumber";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = await dbConnection.QueryAsync<SyncTwitterUser>(query);
var result = await dbConnection.QueryAsync<SyncTwitterUser>(query, new { maxNumber });
return result.ToArray();
}
}
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId)
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, DateTime lastSync)
{
if(id == default) throw new ArgumentException("id");
if(lastTweetPostedId == default) throw new ArgumentException("lastTweetPostedId");
if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId");
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId WHERE id = @id";
if(lastSync == default) throw new ArgumentException("lastSync");
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId, lastSync = @lastSync WHERE id = @id";
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId });
await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, lastSync = lastSync.ToUniversalTime() });
}
}

View file

@ -18,18 +18,11 @@ namespace BirdsiteLive.DAL.Postgres.Tools
public async Task ExecuteRequestAsync(string request)
{
try
using (var conn = new NpgsqlConnection(_settings.ConnString))
using (var cmd = new NpgsqlCommand(request, conn))
{
using (var conn = new NpgsqlConnection(_settings.ConnString))
using (var cmd = new NpgsqlCommand(request, conn))
{
await conn.OpenAsync();
await cmd.ExecuteNonQueryAsync();
}
}
catch (Exception e)
{
Console.WriteLine(e);
await conn.OpenAsync();
await cmd.ExecuteNonQueryAsync();
}
}
}

View file

@ -9,7 +9,7 @@ namespace BirdsiteLive.DAL.Contracts
Task<Version> GetCurrentDbVersionAsync();
Version GetMandatoryDbVersion();
Tuple<Version, Version>[] GetMigrationPatterns();
Task MigrateDbAsync(Version from, Version to);
Task InitDbAsync();
Task<Version> MigrateDbAsync(Version from, Version to);
Task<Version> InitDbAsync();
}
}

View file

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Models;
namespace BirdsiteLive.DAL.Contracts
@ -7,8 +8,8 @@ namespace BirdsiteLive.DAL.Contracts
{
Task CreateTwitterUserAsync(string acct, long lastTweetPostedId);
Task<SyncTwitterUser> GetTwitterUserAsync(string acct);
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync();
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId);
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync(int maxNumber);
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, DateTime lastSync);
Task DeleteTwitterUserAsync(string acct);
Task<int> GetTwitterUsersCountAsync();
}

View file

@ -0,0 +1,46 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
namespace BirdsiteLive.DAL
{
public interface IDatabaseInitializer
{
Task InitAndMigrateDbAsync();
}
public class DatabaseInitializer : IDatabaseInitializer
{
private readonly IDbInitializerDal _dbInitializerDal;
#region Ctor
public DatabaseInitializer(IDbInitializerDal dbInitializerDal)
{
_dbInitializerDal = dbInitializerDal;
}
#endregion
public async Task InitAndMigrateDbAsync()
{
var currentVersion = await _dbInitializerDal.GetCurrentDbVersionAsync();
var mandatoryVersion = _dbInitializerDal.GetMandatoryDbVersion();
if (currentVersion == mandatoryVersion) return;
// Init Db
if (currentVersion == null)
currentVersion = await _dbInitializerDal.InitDbAsync();
// Migrate Db
var migrationPatterns = _dbInitializerDal.GetMigrationPatterns();
while (migrationPatterns.Any(x => x.Item1 == currentVersion))
{
var migration = migrationPatterns.First(x => x.Item1 == currentVersion);
currentVersion = await _dbInitializerDal.MigrateDbAsync(migration.Item1, migration.Item2);
}
if (currentVersion != mandatoryVersion) throw new Exception("Migrating DB failed");
}
}
}

View file

@ -1,4 +1,6 @@
namespace BirdsiteLive.DAL.Models
using System;
namespace BirdsiteLive.DAL.Models
{
public class SyncTwitterUser
{
@ -7,5 +9,7 @@
public long LastTweetPostedId { get; set; }
public long LastTweetSynchronizedForAllFollowersId { get; set; }
public DateTime LastSync { get; set; }
}
}

View file

@ -14,21 +14,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]
public async Task CleanUp()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
try
{
await dal.DeleteAllAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
await dal.DeleteAllAsync();
}
[TestMethod]

View file

@ -17,17 +17,14 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
{
await dal.DeleteAllAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
catch (Exception ) { }
}
[TestMethod]
public async Task GetCurrentDbVersionAsync_UninitializedDb()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
var current = await dal.GetCurrentDbVersionAsync();
Assert.IsNull(current);
}
@ -35,11 +32,11 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
[TestMethod]
public async Task InitDbAsync()
{
var mandatory = new Version(1, 0);
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var current = await dal.GetCurrentDbVersionAsync();
var mandatory = dal.GetMandatoryDbVersion();
Assert.IsNotNull(current);
Assert.AreEqual(mandatory.Minor, current.Minor);
Assert.AreEqual(mandatory.Major, current.Major);

View file

@ -16,21 +16,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]
public async Task CleanUp()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
try
{
await dal.DeleteAllAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
await dal.DeleteAllAsync();
}
[TestMethod]
@ -38,7 +32,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
{
var acct = "myhandle";
var host = "domain.ext";
var following = new[] {12, 19, 23};
var following = new[] { 12, 19, 23 };
var followingSync = new Dictionary<int, long>()
{
{12, 165L},
@ -47,7 +41,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
};
var inboxRoute = "/myhandle/inbox";
var sharedInboxRoute = "/inbox";
var dal = new FollowersPostgresDal(_settings);
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
@ -105,7 +99,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
//User 1
var acct = "myhandle1";
var host = "domain.ext";
var following = new[] { 1,2,3 };
var following = new[] { 1, 2, 3 };
var followingSync = new Dictionary<int, long>();
var inboxRoute = "/myhandle1/inbox";
var sharedInboxRoute = "/inbox";
@ -202,7 +196,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
};
result.Followings = updatedFollowing.ToList();
result.FollowingsSyncStatus = updatedFollowingSync;
await dal.UpdateFollowerAsync(result);
result = await dal.GetFollowerAsync(acct, host);

View file

@ -14,21 +14,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]
public async Task CleanUp()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
try
{
await dal.DeleteAllAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
await dal.DeleteAllAsync();
}
[TestMethod]
@ -70,13 +64,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
var updatedLastTweetId = 1600L;
var updatedLastSyncId = 1550L;
await dal.UpdateTwitterUserAsync(result.Id, updatedLastTweetId, updatedLastSyncId);
var now = DateTime.Now;
await dal.UpdateTwitterUserAsync(result.Id, updatedLastTweetId, updatedLastSyncId, now);
result = await dal.GetTwitterUserAsync(acct);
Assert.AreEqual(acct, result.Acct);
Assert.AreEqual(updatedLastTweetId, result.LastTweetPostedId);
Assert.AreEqual(updatedLastSyncId, result.LastTweetSynchronizedForAllFollowersId);
Assert.IsTrue(Math.Abs((now.ToUniversalTime() - result.LastSync).Milliseconds) < 100);
}
[TestMethod]
@ -108,7 +104,7 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
await dal.CreateTwitterUserAsync(acct, lastTweetId);
}
var result = await dal.GetAllTwitterUsersAsync();
var result = await dal.GetAllTwitterUsersAsync(1000);
Assert.AreEqual(1000, result.Length);
Assert.IsFalse(result[0].Id == default);
Assert.IsFalse(result[0].Acct == default);
@ -116,6 +112,41 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default);
}
[TestMethod]
public async Task GetAllTwitterUsers_Limited()
{
var now = DateTime.Now;
var oldest = now.AddDays(-3);
var newest = now.AddDays(-2);
var dal = new TwitterUserPostgresDal(_settings);
for (var i = 0; i < 20; i++)
{
var acct = $"myid{i}";
var lastTweetId = 1548L;
await dal.CreateTwitterUserAsync(acct, lastTweetId);
}
var allUsers = await dal.GetAllTwitterUsersAsync(100);
for (var i = 0; i < 20; i++)
{
var user = allUsers[i];
var date = i % 2 == 0 ? oldest : newest;
await dal.UpdateTwitterUserAsync(user.Id, user.LastTweetPostedId, user.LastTweetSynchronizedForAllFollowersId, date);
}
var result = await dal.GetAllTwitterUsersAsync(10);
Assert.AreEqual(10, result.Length);
Assert.IsFalse(result[0].Id == default);
Assert.IsFalse(result[0].Acct == default);
Assert.IsFalse(result[0].LastTweetPostedId == default);
Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default);
foreach (var acc in result)
Assert.IsTrue(Math.Abs((acc.LastSync - oldest.ToUniversalTime()).TotalMilliseconds) < 1000);
}
[TestMethod]
public async Task CountTwitterUsers()
{

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\DataAccessLayers\BirdsiteLive.DAL\BirdsiteLive.DAL.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,240 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace BirdsiteLive.DAL.Tests
{
[TestClass]
public class DatabaseInitializerTests
{
[TestMethod]
public async Task DbInitAsync_UpToDate_Test()
{
#region Stubs
var current = new Version(2, 3);
var mandatory = new Version(2, 3);
#endregion
#region Mocks
var dbInitializerDal = new Mock<IDbInitializerDal>(MockBehavior.Strict);
dbInitializerDal
.Setup(x => x.GetCurrentDbVersionAsync())
.ReturnsAsync(current);
dbInitializerDal
.Setup(x => x.GetMandatoryDbVersion())
.Returns(mandatory);
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
#endregion
}
[TestMethod]
public async Task DbInitAsync_NoDb_Test()
{
#region Stubs
var current = (Version)null;
var mandatory = new Version(1, 0);
var migrationPatterns = new Tuple<Version, Version>[0];
#endregion
#region Mocks
var dbInitializerDal = new Mock<IDbInitializerDal>(MockBehavior.Strict);
dbInitializerDal
.Setup(x => x.GetCurrentDbVersionAsync())
.ReturnsAsync(current);
dbInitializerDal
.Setup(x => x.GetMandatoryDbVersion())
.Returns(mandatory);
dbInitializerDal
.Setup(x => x.InitDbAsync())
.ReturnsAsync(new Version(1, 0));
dbInitializerDal
.Setup(x => x.GetMigrationPatterns())
.Returns(migrationPatterns);
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
#endregion
}
[TestMethod]
public async Task DbInitAsync_NoDb_Migration_Test()
{
#region Stubs
var current = (Version)null;
var mandatory = new Version(2, 3);
var migrationPatterns = new Tuple<Version, Version>[]
{
new Tuple<Version, Version>(new Version(1,0), new Version(1,7)),
new Tuple<Version, Version>(new Version(1,7), new Version(2,0)),
new Tuple<Version, Version>(new Version(2,0), new Version(2,3))
};
#endregion
#region Mocks
var dbInitializerDal = new Mock<IDbInitializerDal>(MockBehavior.Strict);
dbInitializerDal
.Setup(x => x.GetCurrentDbVersionAsync())
.ReturnsAsync(current);
dbInitializerDal
.Setup(x => x.GetMandatoryDbVersion())
.Returns(mandatory);
dbInitializerDal
.Setup(x => x.InitDbAsync())
.ReturnsAsync(new Version(1, 0));
dbInitializerDal
.Setup(x => x.GetMigrationPatterns())
.Returns(migrationPatterns);
foreach (var m in migrationPatterns)
{
dbInitializerDal
.Setup(x => x.MigrateDbAsync(
It.Is<Version>(y => y == m.Item1),
It.Is<Version>(y => y == m.Item2)
))
.ReturnsAsync(m.Item2);
}
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
#endregion
}
[TestMethod]
public async Task DbInitAsync_HasDb_Migration_Test()
{
#region Stubs
var current = new Version(1, 7);
var mandatory = new Version(2, 3);
var migrationPatterns = new Tuple<Version, Version>[]
{
new Tuple<Version, Version>(new Version(1,0), new Version(1,7)),
new Tuple<Version, Version>(new Version(1,7), new Version(2,0)),
new Tuple<Version, Version>(new Version(2,0), new Version(2,3))
};
#endregion
#region Mocks
var dbInitializerDal = new Mock<IDbInitializerDal>(MockBehavior.Strict);
dbInitializerDal
.Setup(x => x.GetCurrentDbVersionAsync())
.ReturnsAsync(current);
dbInitializerDal
.Setup(x => x.GetMandatoryDbVersion())
.Returns(mandatory);
dbInitializerDal
.Setup(x => x.GetMigrationPatterns())
.Returns(migrationPatterns);
foreach (var m in migrationPatterns.Skip(1))
{
dbInitializerDal
.Setup(x => x.MigrateDbAsync(
It.Is<Version>(y => y == m.Item1),
It.Is<Version>(y => y == m.Item2)
))
.ReturnsAsync(m.Item2);
}
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
#endregion
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public async Task DbInitAsync_NoDb_Migration_Error_Test()
{
#region Stubs
var current = (Version)null;
var mandatory = new Version(2, 3);
var migrationPatterns = new Tuple<Version, Version>[]
{
new Tuple<Version, Version>(new Version(1,0), new Version(1,7)),
new Tuple<Version, Version>(new Version(1,7), new Version(2,0)),
new Tuple<Version, Version>(new Version(2,0), new Version(2,2))
};
#endregion
#region Mocks
var dbInitializerDal = new Mock<IDbInitializerDal>(MockBehavior.Strict);
dbInitializerDal
.Setup(x => x.GetCurrentDbVersionAsync())
.ReturnsAsync(current);
dbInitializerDal
.Setup(x => x.GetMandatoryDbVersion())
.Returns(mandatory);
dbInitializerDal
.Setup(x => x.InitDbAsync())
.ReturnsAsync(new Version(1, 0));
dbInitializerDal
.Setup(x => x.GetMigrationPatterns())
.Returns(migrationPatterns);
foreach (var m in migrationPatterns)
{
dbInitializerDal
.Setup(x => x.MigrateDbAsync(
It.Is<Version>(y => y == m.Item1),
It.Is<Version>(y => y == m.Item2)
))
.ReturnsAsync(m.Item2);
}
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
try
{
await dbInitializer.InitAndMigrateDbAsync();
}
finally
{
#region Validations
dbInitializerDal.VerifyAll();
#endregion
}
}
}
}

View file

@ -18,4 +18,8 @@
<ProjectReference Include="..\..\BirdsiteLive.Pipeline\BirdsiteLive.Pipeline.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Tools\" />
</ItemGroup>
</Project>

View file

@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -54,7 +55,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
.Setup(x => x.UpdateTwitterUserAsync(
It.Is<int>(y => y == user1.Id),
It.Is<long>(y => y == tweets.Last().Id),
It.Is<long>(y => y == tweets.Last().Id)
It.Is<long>(y => y == tweets.Last().Id),
It.IsAny<DateTime>()
))
.Returns(Task.CompletedTask);
#endregion

View file

@ -3,9 +3,11 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Processors;
using BirdsiteLive.Pipeline.Tools;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@ -26,24 +28,32 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
new SyncTwitterUser(),
new SyncTwitterUser(),
};
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.Setup(x => x.GetAllTwitterUsersAsync(
It.Is<int>(y => y == maxUsers)))
.ReturnsAsync(users);
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 10;
processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
await Task.Delay(50);
#region Validations
maxUsersNumberProviderMock.VerifyAll();
twitterUserDalMock.VerifyAll();
Assert.AreEqual(3, buffer.Count);
buffer.TryReceive(out var result);
@ -60,25 +70,37 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
for (var i = 0; i < 30; i++)
users.Add(new SyncTwitterUser());
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.SetupSequence(x => x.GetAllTwitterUsersAsync())
.SetupSequence(x => x.GetAllTwitterUsersAsync(
It.Is<int>(y => y == maxUsers)))
.ReturnsAsync(users.ToArray())
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0]);
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 2;
processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
await Task.Delay(300);
#region Validations
maxUsersNumberProviderMock.VerifyAll();
twitterUserDalMock.VerifyAll();
Assert.AreEqual(15, buffer.Count);
buffer.TryReceive(out var result);
@ -95,25 +117,37 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
for (var i = 0; i < 31; i++)
users.Add(new SyncTwitterUser());
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.SetupSequence(x => x.GetAllTwitterUsersAsync())
.SetupSequence(x => x.GetAllTwitterUsersAsync(
It.Is<int>(y => y == maxUsers)))
.ReturnsAsync(users.ToArray())
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0])
.ReturnsAsync(new SyncTwitterUser[0]);
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 2;
processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
await Task.Delay(200);
#region Validations
maxUsersNumberProviderMock.VerifyAll();
twitterUserDalMock.VerifyAll();
Assert.AreEqual(11, buffer.Count);
buffer.TryReceive(out var result);
@ -126,24 +160,33 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
{
#region Stubs
var buffer = new BufferBlock<SyncTwitterUser[]>();
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.Setup(x => x.GetAllTwitterUsersAsync(
It.Is<int>(y => y == maxUsers)))
.ReturnsAsync(new SyncTwitterUser[0]);
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 1;
processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
await Task.Delay(50);
#region Validations
maxUsersNumberProviderMock.VerifyAll();
twitterUserDalMock.VerifyAll();
Assert.AreEqual(0, buffer.Count);
#endregion
@ -154,24 +197,33 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
{
#region Stubs
var buffer = new BufferBlock<SyncTwitterUser[]>();
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetAllTwitterUsersAsync())
.Setup(x => x.GetAllTwitterUsersAsync(
It.Is<int>(y => y == maxUsers)))
.Returns(async () => await DelayFaultedTask<SyncTwitterUser[]>(new Exception()));
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 10;
var t = processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
await Task.WhenAny(t, Task.Delay(50));
#region Validations
maxUsersNumberProviderMock.VerifyAll();
twitterUserDalMock.VerifyAll();
Assert.AreEqual(0, buffer.Count);
#endregion
@ -185,14 +237,22 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
var buffer = new BufferBlock<SyncTwitterUser[]>();
var canTokenS = new CancellationTokenSource();
canTokenS.Cancel();
var maxUsers = 1000;
#endregion
#region Mocks
var maxUsersNumberProviderMock = new Mock<IMaxUsersNumberProvider>(MockBehavior.Strict);
maxUsersNumberProviderMock
.Setup(x => x.GetMaxUsersNumberAsync())
.ReturnsAsync(maxUsers);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
var loggerMock = new Mock<ILogger<RetrieveTwitterUsersProcessor>>();
#endregion
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, loggerMock.Object);
var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
processor.WaitFactor = 1;
await processor.GetTwitterUsersAsync(buffer, canTokenS.Token);
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
@ -60,7 +61,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
.Setup(x => x.UpdateTwitterUserAsync(
It.Is<int>(y => y == user.Id),
It.Is<long>(y => y == tweet2.Id),
It.Is<long>(y => y == tweet2.Id)
It.Is<long>(y => y == tweet2.Id),
It.IsAny<DateTime>()
))
.Returns(Task.CompletedTask);
#endregion
@ -123,7 +125,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
.Setup(x => x.UpdateTwitterUserAsync(
It.Is<int>(y => y == user.Id),
It.Is<long>(y => y == tweet3.Id),
It.Is<long>(y => y == tweet2.Id)
It.Is<long>(y => y == tweet2.Id),
It.IsAny<DateTime>()
))
.Returns(Task.CompletedTask);
#endregion
@ -194,7 +197,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
.Setup(x => x.UpdateTwitterUserAsync(
It.Is<int>(y => y == user.Id),
It.Is<long>(y => y == tweet3.Id),
It.Is<long>(y => y == tweet2.Id)
It.Is<long>(y => y == tweet2.Id),
It.IsAny<DateTime>()
))
.Returns(Task.CompletedTask);
#endregion

View file

@ -0,0 +1,79 @@
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Pipeline.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace BirdsiteLive.Pipeline.Tests.Tools
{
[TestClass]
public class MaxUsersNumberProviderTests
{
[TestMethod]
public async Task GetMaxUsersNumberAsync_WarmUp_Test()
{
#region Stubs
var settings = new InstanceSettings
{
MaxUsersCapacity = 1000
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetTwitterUsersCountAsync())
.ReturnsAsync(1000);
#endregion
var provider = new MaxUsersNumberProvider(settings, twitterUserDalMock.Object);
var result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(250, result);
result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(250, result);
result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(250, result);
result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(250, result);
result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(1000, result);
#region Validations
twitterUserDalMock.VerifyAll();
#endregion
}
[TestMethod]
public async Task GetMaxUsersNumberAsync_NoWarmUp_Test()
{
#region Stubs
var settings = new InstanceSettings
{
MaxUsersCapacity = 1000
};
#endregion
#region Mocks
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
.Setup(x => x.GetTwitterUsersCountAsync())
.ReturnsAsync(249);
#endregion
var provider = new MaxUsersNumberProvider(settings, twitterUserDalMock.Object);
var result = await provider.GetMaxUsersNumberAsync();
Assert.AreEqual(1000, result);
#region Validations
twitterUserDalMock.VerifyAll();
#endregion
}
}
}