public async Task TestAddAccountAndSession()
        {
            await AsyncParallel.ForAsync(0, 100, async i =>
            {
                using IServiceScope scope = serviceProvider.CreateScope();
                var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                (var account, _, bool success) = await database
                                                 .AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false);
                Assert.IsTrue(success);

                await AsyncParallel.ForAsync(0, 10, async j =>
                {
                    using IServiceScope scope = serviceProvider.CreateScope();
                    var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                    Session session = new Session()
                    {
                        AccountId             = account.AccountId,
                        SessionTokenHash      = SkynetRandom.Bytes(32), // No real hashes needed here
                        WebTokenHash          = SkynetRandom.Bytes(32),
                        ApplicationIdentifier = "windows/SkynetServer.Database.Tests"
                    };
                    await database.AddSession(session).ConfigureAwait(false);
                }).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        public async Task TestAddChannelAndMessage()
        {
            await AsyncParallel.ForAsync(0, 5, async i =>
            {
                using IServiceScope scope = serviceProvider.CreateScope();
                var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                Channel channel = new Channel()
                {
                    ChannelType = ChannelType.Loopback
                };
                await database.AddChannel(channel).ConfigureAwait(false);

                await AsyncParallel.ForAsync(0, 100, async j =>
                {
                    using IServiceScope scope = serviceProvider.CreateScope();
                    var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                    Message message = new Message()
                    {
                        ChannelId    = channel.ChannelId,
                        MessageFlags = MessageFlags.Unencrypted
                    };
                    database.Messages.Add(message);
                    await database.SaveChangesAsync().ConfigureAwait(false);
                }).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
 public async Task TestAddAccount()
 {
     await AsyncParallel.ForAsync(0, 500, async i =>
     {
         using IServiceScope scope = serviceProvider.CreateScope();
         var database         = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
         (_, _, bool success) = await database.AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false);
         Assert.IsTrue(success);
     }).ConfigureAwait(false);
 }
        public async Task TestAddChannel()
        {
            await AsyncParallel.ForAsync(0, 500, async i =>
            {
                using IServiceScope scope = serviceProvider.CreateScope();
                var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                Channel channel = new Channel()
                {
                    ChannelType = ChannelType.Loopback
                };
                await database.AddChannel(channel).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        public async Task TestAddChannelWithOwner()
        {
            await AsyncParallel.ForAsync(0, 50, async i =>
            {
                using IServiceScope scope = serviceProvider.CreateScope();
                var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                (var account, _, bool success) = await database
                                                 .AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false);
                Assert.IsTrue(success);

                Channel channel = new Channel()
                {
                    OwnerId     = account.AccountId,
                    ChannelType = ChannelType.Loopback
                };
                await database.AddChannel(channel).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        public async Task TestAddMessageAndDependency()
        {
            using IServiceScope scope = serviceProvider.CreateScope();
            var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

            Channel channel = new Channel()
            {
                ChannelType = ChannelType.Loopback
            };
            await database.AddChannel(channel).ConfigureAwait(false);

            Message previous = null;

            await AsyncParallel.ForAsync(0, 100, async i =>
            {
                using IServiceScope scope = serviceProvider.CreateScope();
                var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();

                var dependencies = new List <MessageDependency>();
                if (previous != null)
                {
                    dependencies.Add(new MessageDependency {
                        MessageId = previous.MessageId
                    });
                }

                Message message = new Message()
                {
                    ChannelId    = channel.ChannelId,
                    MessageFlags = MessageFlags.Unencrypted,
                    Dependencies = dependencies
                };
                database.Messages.Add(message);
                await database.SaveChangesAsync().ConfigureAwait(false);
                previous = message;
            }).ConfigureAwait(false);
        }
Exemplo n.º 7
0
            private async Task OnExecute(IConsole console, IServiceProvider provider)
            {
                long      accountId, channelId;
                Stopwatch stopwatch = new Stopwatch();

                using (IServiceScope scope = provider.CreateScope())
                {
                    DatabaseContext database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
                    (var account, var confirmation, bool success) = await database
                                                                    .AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false);

                    accountId = account.AccountId;
                    console.Out.WriteLine($"Created account {confirmation.MailAddress} with ID {accountId}");
                    console.Out.WriteLine($"Created mail confirmation for {confirmation.MailAddress} with token {confirmation.Token}");
                }

                using (IServiceScope scope = provider.CreateScope())
                {
                    DatabaseContext database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
                    Channel         channel  = await database.AddChannel(new Channel()
                    {
                        OwnerId = accountId
                    }).ConfigureAwait(false);

                    channelId = channel.ChannelId;
                    console.Out.WriteLine($"Created channel {channelId} with owner {accountId}");
                }

                if (AccountCount > 0)
                {
                    console.Out.WriteLine($"Inserting {AccountCount} accounts...");
                    stopwatch.Start();

                    await AsyncParallel.ForAsync(0, AccountCount, async i =>
                    {
                        using IServiceScope scope = provider.CreateScope();
                        DatabaseContext ctx       = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
                        await ctx.AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false);
                    }).ConfigureAwait(false);

                    stopwatch.Stop();
                    console.Out.WriteLine($"Finished saving {AccountCount} accounts after {stopwatch.ElapsedMilliseconds}ms");
                    stopwatch.Reset();
                }

                if (MessageCount > 0)
                {
                    console.Out.WriteLine($"Inserting {MessageCount} messages to channel {channelId}...");
                    stopwatch.Start();

                    await AsyncParallel.ForAsync(0, MessageCount, async i =>
                    {
                        using IServiceScope scope = provider.CreateScope();
                        DatabaseContext database  = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
                        Message entity            = new Message {
                            ChannelId = channelId, SenderId = accountId
                        };
                        database.Messages.Add(entity);
                        await database.SaveChangesAsync().ConfigureAwait(false);
                    }).ConfigureAwait(false);

                    stopwatch.Stop();
                    console.Out.WriteLine($"Finished saving {MessageCount} messages after {stopwatch.ElapsedMilliseconds}ms");
                    stopwatch.Reset();
                }
            }