Ejemplo n.º 1
0
        //Initialize values
        private void Initialize()
        {
            // MySqlCredentials.GetMySqlConnection() is not present in the git repository for it contains sensitive data.
            IMySqlCredentials credentials = new MySqlCredentials();

            connection = credentials.GetMySqlConnection();
        }
Ejemplo n.º 2
0
 public TestingMySqlDb() :
     this(MySqlCredentials.GetConnectionString(TestContext.CurrentContext.TestDirectory))
 {
 }
Ejemplo n.º 3
0
        public static int Main(string[] args)
        {
            var         type = args[0];
            var         name = args[1];
            IDisposable?handle;

            switch (type)
            {
            case nameof(SqlDistributedLock):
                handle = new SqlDistributedLock(name, SqlServerCredentials.ConnectionString).Acquire();
                break;

            case "Write" + nameof(SqlDistributedReaderWriterLock):
                handle = new SqlDistributedReaderWriterLock(name, SqlServerCredentials.ConnectionString).AcquireWriteLock();
                break;

            case nameof(SqlDistributedSemaphore) + "1AsMutex":
                handle = new SqlDistributedSemaphore(name, maxCount: 1, connectionString: SqlServerCredentials.ConnectionString).Acquire();
                break;

            case nameof(SqlDistributedSemaphore) + "5AsMutex":
                handle = new SqlDistributedSemaphore(name, maxCount: 5, connectionString: SqlServerCredentials.ConnectionString).Acquire();
                break;

            case nameof(PostgresDistributedLock):
                handle = new PostgresDistributedLock(new PostgresAdvisoryLockKey(name), PostgresCredentials.GetConnectionString(Environment.CurrentDirectory)).Acquire();
                break;

            case "Write" + nameof(PostgresDistributedReaderWriterLock):
                handle = new PostgresDistributedReaderWriterLock(new PostgresAdvisoryLockKey(name), PostgresCredentials.GetConnectionString(Environment.CurrentDirectory)).AcquireWriteLock();
                break;

            case nameof(MySqlDistributedLock):
                handle = new MySqlDistributedLock(name, MySqlCredentials.GetConnectionString(Environment.CurrentDirectory)).Acquire();
                break;

            case "MariaDB" + nameof(MySqlDistributedLock):
                handle = new MySqlDistributedLock(name, MariaDbCredentials.GetConnectionString(Environment.CurrentDirectory)).Acquire();
                break;

            case nameof(OracleDistributedLock):
                handle = new OracleDistributedLock(name, OracleCredentials.GetConnectionString(Environment.CurrentDirectory)).Acquire();
                break;

            case "Write" + nameof(OracleDistributedReaderWriterLock):
                handle = new OracleDistributedReaderWriterLock(name, OracleCredentials.GetConnectionString(Environment.CurrentDirectory)).AcquireWriteLock();
                break;

            case nameof(EventWaitHandleDistributedLock):
                handle = new EventWaitHandleDistributedLock(name).Acquire();
                break;

            case nameof(WaitHandleDistributedSemaphore) + "1AsMutex":
                handle = new WaitHandleDistributedSemaphore(name, maxCount: 1).Acquire();
                break;

            case nameof(WaitHandleDistributedSemaphore) + "5AsMutex":
                handle = new WaitHandleDistributedSemaphore(name, maxCount: 5).Acquire();
                break;

            case nameof(AzureBlobLeaseDistributedLock):
                handle = new AzureBlobLeaseDistributedLock(
                    new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name),
                    o => o.Duration(TimeSpan.FromSeconds(15))
                    )
                         .Acquire();
                break;

            case nameof(FileDistributedLock):
                handle = new FileDistributedLock(new FileInfo(name)).Acquire();
                break;

            case nameof(RedisDistributedLock) + "1":
                handle = AcquireRedisLock(name, serverCount: 1);
                break;

            case nameof(RedisDistributedLock) + "3":
                handle = AcquireRedisLock(name, serverCount: 3);
                break;

            case nameof(RedisDistributedLock) + "2x1":
                handle = AcquireRedisLock(name, serverCount: 2);     // we know the last will fail; don't bother (we also don't know its port)
                break;

            case nameof(RedisDistributedLock) + "1WithPrefix":
                handle = AcquireRedisLock("distributed_locks:" + name, serverCount: 1);
                break;

            case "Write" + nameof(RedisDistributedReaderWriterLock) + "1":
                handle = AcquireRedisWriteLock(name, serverCount: 1);
                break;

            case "Write" + nameof(RedisDistributedReaderWriterLock) + "3":
                handle = AcquireRedisWriteLock(name, serverCount: 3);
                break;

            case "Write" + nameof(RedisDistributedReaderWriterLock) + "2x1":
                handle = AcquireRedisWriteLock(name, serverCount: 2);     // we know the last will fail; don't bother (we also don't know its port)
                break;

            case "Write" + nameof(RedisDistributedReaderWriterLock) + "1WithPrefix":
                handle = AcquireRedisWriteLock("distributed_locks:" + name, serverCount: 1);
                break;

            case string _ when type.StartsWith(nameof(RedisDistributedSemaphore)):
            {
                var maxCount = type.EndsWith("1AsMutex") ? 1
                            : type.EndsWith("5AsMutex") ? 5
                            : throw new ArgumentException(type);
                handle = new RedisDistributedSemaphore(
                    name,
                    maxCount,
                    GetRedisDatabases(serverCount: 1).Single(),
                    // in order to see abandonment work in a reasonable timeframe, use very short expiry
                    options => options.Expiry(TimeSpan.FromSeconds(1))
                    .BusyWaitSleepTime(TimeSpan.FromSeconds(.1), TimeSpan.FromSeconds(.3))
                    ).Acquire();
                break;
            }

            case nameof(ZooKeeperDistributedLock):
                handle = new ZooKeeperDistributedLock(new ZooKeeperPath(name), ZooKeeperPorts.DefaultConnectionString, options: ZooKeeperOptions).AcquireAsync().Result;
                break;

            case "Write" + nameof(ZooKeeperDistributedReaderWriterLock):
                handle = new ZooKeeperDistributedReaderWriterLock(new ZooKeeperPath(name), ZooKeeperPorts.DefaultConnectionString, options: ZooKeeperOptions).AcquireWriteLockAsync().Result;
                break;

            case string _ when type.StartsWith(nameof(ZooKeeperDistributedSemaphore)):
            {
                var maxCount = type.EndsWith("1AsMutex") ? 1
                            : type.EndsWith("5AsMutex") ? 5
                            : throw new ArgumentException(type);
                handle = new ZooKeeperDistributedSemaphore(
                    new ZooKeeperPath(name),
                    maxCount,
                    ZooKeeperPorts.DefaultConnectionString,
                    options: ZooKeeperOptions
                    ).AcquireAsync().Result;
                break;
            }

            default:
                Console.Error.WriteLine($"type: {type}");
                return(123);
            }

            Console.WriteLine("Acquired");
            Console.Out.Flush();

            if (Console.ReadLine() != "abandon")
            {
                handle.Dispose();
            }

            return(0);
        }