Danny
Danny

Reputation: 100

How can I have multiple database instances for component tests

Currently I have a common database used for component tests in xunit, the problem is that when a build is running on teamcity other builds cannot start as while one build is being run a number of tests are being executed which take quite some time. I need some help of how can I have my current class modified/changed in order to have multiple builds executing in parallel using multiple database instances.

public class DatabaseFixture : IDisposable
    {
        public TestCluster Cluster { get; }
        public IGrainFactory GrainFactory => Cluster?.GrainFactory;
        public IClusterClient Client => Cluster?.Client;

        private const string _databaseConnection = @"Server=(localdb)\MSSQLLocalDB;Integrated Security=true";

            public DatabaseFixture()
        {
            // Find a valid folder for now
            foreach (var folder in _dbFolders)
            {
                if (!Directory.Exists(folder))
                    continue;
                _databaseRootFolder = folder;
                break;
            }
            Assert.NotNull(_databaseRootFolder);

            // Create the test database
            CreateDatabase();
            CreateConfigDatabase();

            // Setup and start the cluster
            var builder = new TestClusterBuilder(2);
            builder.AddSiloBuilderConfigurator<DbTestSiloBuilderConfigurator>();

            var testCluster = builder.Build();
            if (testCluster?.Primary == null)
            {
                testCluster?.Deploy();
            }
            Cluster = testCluster;
        }
        
        // other methods that follow have been removed for keeping question short
    }

Upvotes: 1

Views: 232

Answers (1)

Lukasz Nowakowski
Lukasz Nowakowski

Reputation: 336

I may be missing something, but in during build you should only run unit tests. And unit tests shouldn't need database connection.

This means one of two things:

  1. What you're testing is an integration test, which should be ran on a test environment, not on build agent.
  2. You have bad design of the fixture under test and should refactor it.

Upvotes: 1

Related Questions