Skip to content

Bug fix Release - Reuse Containers

Compare
Choose a tag to compare
@mariotoffia mariotoffia released this 31 May 13:49
· 349 commits to master since this release

This is a small bug fix release where the service hooks was not invoked when ReuseIfExist was enabled and the container was reused. Now it will add the hooks to those as well and thus it will drive the State flow and execute any hooks attached. For example the below example would not invoke the CheckConnection prior this release.

Cheers,
Mario

    public void WaitLambdaWithReusedContainerShallGetInvoked()
    {
      _checkConnectionInvoked = false;
      using (var c1 = Fd.UseContainer()
        .UseImage("postgres:9.6-alpine")
        .WithName("postgres")
        .ExposePort(5432)
        .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
        .ReuseIfExists()
        .WaitForPort("5432/tcp", TimeSpan.FromSeconds(30))
        .Build()
        .Start())
      {
        // Make sure to have named container running
        var config = c1.GetConfiguration();
        AreEqual(ServiceRunningState.Running, c1.State);

        using (var c2 = Fd.UseContainer()
          .UseImage("postgres:9.6-alpine")
          .WithName("postgres")
          .ExposePort(5432)
          .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
          .ReuseIfExists()
          .Wait("", (service, count) => CheckConnection(count, service))
          .Build()
          .Start())
        {
          IsTrue(_checkConnectionInvoked,
            "Is invoked even if reused container since Start drives the container state eventually to running");
        }
      }
    }

    private static int CheckConnection(int count, IContainerService service)
    {
      _checkConnectionInvoked = true;

      if (count > 10) throw new FluentDockerException("Failed to wait for sql server");

      var ep = service.ToHostExposedEndpoint("5432/tcp");
      var str = $"Server={ep.Address};Port={ep.Port};Userid=postgres;" +
                "Password=mysecretpassword;Pooling=false;MinPoolSize=1;" +
                "MaxPoolSize=20;Timeout=15;SslMode=Disable;Database=postgres";

      try
      {
        using (var conn = new NpgsqlConnection(str))
        {
          conn.Open();
          return 0;
        }
      }
      catch
      {
        return 500 /*ms*/;
      }
    }