Exemplo n.º 1
0
        public async Task <ProviderRun> RunAsync(ProviderFixture serverFixture, string[] tables = null,
                                                 Action <SyncConfiguration> conf = null, bool reuseAgent = true)
        {
            // server proxy
            var proxyClientProvider = new WebProxyClientProvider();

            var syncTables = tables ?? serverFixture.Tables;

            // local test, through tcp
            if (this.NetworkType == NetworkType.Tcp)
            {
                // create agent
                if (this.Agent == null || !reuseAgent)
                {
                    this.Agent = new SyncAgent(this.ClientProvider, serverFixture.ServerProvider, syncTables);
                }

                // copy conf settings
                if (conf != null)
                {
                    this.Agent.SetConfiguration(conf);
                }

                // Add Filers
                if (serverFixture.Filters != null && serverFixture.Filters.Count > 0)
                {
                    serverFixture.Filters.ForEach(f =>
                    {
                        if (!this.Agent.LocalProvider.Configuration.Filters.Contains(f))
                        {
                            this.Agent.LocalProvider.Configuration.Filters.Add(f);
                        }
                    });
                }

                // Add Filers values
                if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                {
                    foreach (var syncParam in serverFixture.FilterParameters)
                    {
                        if (!this.Agent.Parameters.Contains(syncParam))
                        {
                            this.Agent.Parameters.Add(syncParam);
                        }
                    }
                }

                // sync
                try
                {
                    this.BeginRun?.Invoke(this.Agent.RemoteProvider);
                    this.Results = await this.Agent.SynchronizeAsync();

                    this.EndRun?.Invoke(this.Agent.RemoteProvider);
                }
                catch (Exception ex)
                {
                    this.Exception = ex;
                    Console.WriteLine(ex);
                }
            }

            // -----------------------------------------------------------------------
            // HTTP
            // -----------------------------------------------------------------------

            // tests through http proxy
            if (this.NetworkType == NetworkType.Http)
            {
                using (var server = new KestrellTestServer())
                {
                    // server handler
                    var serverHandler = new RequestDelegate(async context =>
                    {
                        // test if <> directory name works
                        var options = new Action <SyncOptions>(o => o.BatchDirectory = Path.Combine(SyncOptions.GetDefaultUserBatchDiretory(), "server"));

                        // sync
                        try
                        {
                            var proxyServerProvider = WebProxyServerProvider.Create(
                                context, serverFixture.ServerProvider, conf, options);

                            var serverProvider = proxyServerProvider.GetLocalProvider(context);

                            serverProvider.Configuration.Add(syncTables);

                            this.BeginRun?.Invoke(serverProvider);
                            await proxyServerProvider.HandleRequestAsync(context);
                            this.EndRun?.Invoke(serverProvider);
                        }
                        catch (Exception ew)
                        {
                            Console.WriteLine(ew);
                        }
                    });

                    var clientHandler = new ResponseDelegate(async(serviceUri) =>
                    {
                        // create agent
                        if (this.Agent == null || !reuseAgent)
                        {
                            this.Agent = new SyncAgent(this.ClientProvider, proxyClientProvider);
                        }

                        if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                        {
                            foreach (var syncParam in serverFixture.FilterParameters)
                            {
                                if (!this.Agent.Parameters.Contains(syncParam))
                                {
                                    this.Agent.Parameters.Add(syncParam);
                                }
                            }
                        }

                        ((WebProxyClientProvider)this.Agent.RemoteProvider).ServiceUri = new Uri(serviceUri);

                        try
                        {
                            this.Results = await this.Agent.SynchronizeAsync();
                        }
                        catch (Exception ew)
                        {
                            this.Exception = ew;
                            Console.WriteLine(ew);
                        }
                    });
                    await server.Run(serverHandler, clientHandler);
                }
            }
            return(this);
        }
Exemplo n.º 2
0
        public async Task <ProviderRun> RunAsync(CoreProvider serverProvider, ProviderFixture <CoreProvider> serverFixture, string scopeName = null, string[] tables = null, SyncConfiguration conf = null,
                                                 bool reuseAgent = true)
        {
            // server proxy
            var proxyServerProvider = new WebProxyServerProvider(serverProvider);
            var proxyClientProvider = new WebProxyClientProvider();

            var syncTables = tables ?? serverFixture.Tables;

            // local test, through tcp
            if (NetworkType == NetworkType.Tcp)
            {
                // create agent
                if (this.Agent == null || !reuseAgent)
                {
                    this.Agent = new SyncAgent(ClientProvider, serverProvider, syncTables);
                }

                // copy conf settings
                if (conf != null)
                {
                    serverFixture.CopyConfiguration(this.Agent.Configuration, conf);
                }

                // Add Filers
                if (serverFixture.Filters != null && serverFixture.Filters.Count > 0)
                {
                    serverFixture.Filters.ForEach(f => {
                        if (!Agent.Configuration.Filters.Contains(f))
                        {
                            Agent.Configuration.Filters.Add(f);
                        }
                    });
                }

                // Add Filers values
                if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                {
                    foreach (var syncParam in serverFixture.FilterParameters)
                    {
                        if (!this.Agent.Parameters.Contains(syncParam))
                        {
                            this.Agent.Parameters.Add(syncParam);
                        }
                    }
                }

                // sync
                try
                {
                    BeginRun?.Invoke(this.Agent.RemoteProvider);
                    Results = await this.Agent.SynchronizeAsync();

                    EndRun?.Invoke(this.Agent.RemoteProvider);
                }
                catch (Exception ex)
                {
                    Exception = ex;
                    Console.WriteLine(ex);
                }
            }

            // -----------------------------------------------------------------------
            // HTTP
            // -----------------------------------------------------------------------

            // tests through http proxy
            if (NetworkType == NetworkType.Http)
            {
                // client handler
                using (var server = new KestrellTestServer())
                {
                    // server handler
                    var serverHandler = new RequestDelegate(async context =>
                    {
                        SyncConfiguration syncConfiguration = new SyncConfiguration(syncTables);// set proxy conf
                        // copy conf settings

                        if (conf != null)
                        {
                            serverFixture.CopyConfiguration(syncConfiguration, conf);
                        }


                        // set proxy conf
                        proxyServerProvider.Configuration = syncConfiguration;

                        // Add Filers
                        if (serverFixture.Filters != null && serverFixture.Filters.Count > 0)
                        {
                            serverFixture.Filters.ForEach(f => {
                                if (!proxyServerProvider.Configuration.Filters.Contains(f))
                                {
                                    proxyServerProvider.Configuration.Filters.Add(f);
                                }
                            });
                        }


                        // sync
                        try
                        {
                            BeginRun?.Invoke(proxyServerProvider.LocalProvider);
                            await proxyServerProvider.HandleRequestAsync(context);
                            EndRun?.Invoke(proxyServerProvider.LocalProvider);
                        }
                        catch (Exception ew)
                        {
                            Console.WriteLine(ew);
                        }
                    });

                    var clientHandler = new ResponseDelegate(async(serviceUri) =>
                    {
                        // create agent
                        if (this.Agent == null || !reuseAgent)
                        {
                            this.Agent = new SyncAgent(ClientProvider, proxyClientProvider);
                        }

                        if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                        {
                            foreach (var syncParam in serverFixture.FilterParameters)
                            {
                                if (!this.Agent.Parameters.Contains(syncParam))
                                {
                                    this.Agent.Parameters.Add(syncParam);
                                }
                            }
                        }

                        ((WebProxyClientProvider)this.Agent.RemoteProvider).ServiceUri = new Uri(serviceUri);

                        try
                        {
                            Results = await Agent.SynchronizeAsync();
                        }
                        catch (Exception ew)
                        {
                            Exception = ew;
                            Console.WriteLine(ew);
                        }
                    });
                    await server.Run(serverHandler, clientHandler);
                }
            }
            return(this);
        }