Example #1
0
        public async Task AdapterFill()
        {
            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                var adapter = new SpannerDataAdapter(connection, _testFixture.DataAdapterTestTable, "Key");

                var testDataSet = new DataSet();
                adapter.Fill(testDataSet);
                Assert.Equal(1, testDataSet.Tables.Count);
                Assert.Equal(2, testDataSet.Tables[0].Columns.Count);
                Assert.Equal(_testFixture.TestTableRowCount, testDataSet.Tables[0].Rows.Count);
            }
        }
Example #2
0
        public async Task DistributedReadAsync()
        {
            int numRows;

            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
                using (var cmd = connection.CreateSelectCommand("SELECT COUNT(*) FROM Orders"))
                {
                    numRows = await cmd.ExecuteScalarAsync <int>();
                }

            using (var connection = new SpannerConnection(_testFixture.ConnectionString))
            {
                await connection.OpenAsync();

                using (var transaction = await connection.BeginReadOnlyTransactionAsync())
                    using (var cmd = connection.CreateSelectCommand("SELECT * FROM Orders"))
                    {
                        transaction.DisposeBehavior = DisposeBehavior.CloseResources;
                        cmd.Transaction             = transaction;
                        var partitions = await cmd.GetReaderPartitionsAsync(1000);

                        var transactionId = transaction.TransactionId;

                        //we simulate a serialization/deserialization step in the call to the subtask.
                        await Task.WhenAll(partitions.Select(
                                               x => DistributedReadWorkerAsync(CommandPartition.FromBase64String(x.ToBase64String()),
                                                                               TransactionId.FromBase64String(transactionId.ToBase64String()))))
                        .ConfigureAwait(false);
                    }

                Assert.Equal(numRows, _rowsRead);
            }
        }
Example #3
0
        public async Task TestChunking()
        {
            Logger.Instance.Info($"Seed={_seed}");
            var rowsRead    = 0;
            int rowsToWrite = _random.Next(1, 6);

            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                await connection.OpenAsync();

                using (var tx = await connection.BeginTransactionAsync())
                {
                    using (var cmd = connection.CreateInsertCommand(
                               _testFixture.ChunkingTestTable, new SpannerParameterCollection
                    {
                        new SpannerParameter("K", SpannerDbType.String),
                        new SpannerParameter("StringValue", SpannerDbType.String),
                        new SpannerParameter("StringArrayValue", SpannerDbType.ArrayOf(SpannerDbType.String)),
                        new SpannerParameter("BytesValue", SpannerDbType.Bytes),
                        new SpannerParameter("BytesArrayValue", SpannerDbType.ArrayOf(SpannerDbType.Bytes))
                    }))
                    {
                        cmd.Transaction = tx;

                        //write 1-5 rows
                        for (var i = 0; i < rowsToWrite; i++)
                        {
                            await InsertRowAsync(cmd);
                        }

                        await tx.CommitAsync();
                    }
                }

                using (var readCmd = connection.CreateSelectCommand($"SELECT * FROM {_testFixture.ChunkingTestTable}"))
                {
                    using (var reader = (SpannerDataReader)await readCmd.ExecuteReaderAsync())
                    {
                        var keySet = new HashSet <string>();
                        while (await reader.ReadAsync())
                        {
                            var k = reader.GetFieldValue <string>("K");
                            if (!_addedKeys.Contains(k))
                            {
                                continue; // this key is from a previous test run.
                            }
                            rowsRead++;
                            Assert.True(keySet.Add(k));
                            Assert.Equal(_stringValues[k], reader.GetFieldValue <string>("StringValue"));
                            Assert.Equal(_stringArrayValues[k], reader.GetFieldValue <string[]>("StringArrayValue"));
                            Assert.Equal(_bytesValues[k], reader.GetFieldValue <byte[]>("BytesValue"));
                            Assert.Equal(_bytesArrayValues[k], reader.GetFieldValue <byte[][]>("BytesArrayValue"));
                        }
                    }
                }
            }

            Assert.Equal(rowsToWrite, rowsRead);
        }
Example #4
0
        private async Task <TimeSpan> TestWriteOneRow(Stopwatch sw)
        {
            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                var localCounter  = Interlocked.Increment(ref s_rowCounter);
                var insertCommand = connection.CreateInsertCommand(
                    _testFixture.StressTestTable, new SpannerParameterCollection
                {
                    { "ID", SpannerDbType.String },
                    { "Title", SpannerDbType.String }
                });
                insertCommand.Parameters["ID"].Value    = $"{s_guid}{localCounter}";
                insertCommand.Parameters["Title"].Value = "Title";

                // This uses an ephemeral transaction, so its legal to retry it.
                await ExecuteWithRetry(insertCommand.ExecuteNonQueryAsync);
            }
            return(sw.Elapsed);
        }
Example #5
0
        private async Task TestBind <T>(SpannerDbType parameterType, T value, Func <SpannerDataReader, T> typeSpecificReader = null)
        {
            int rowsRead;
            var valueRead = default(T);

            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                var cmd = connection.CreateSelectCommand(
                    "SELECT @v",
                    new SpannerParameterCollection {
                    new SpannerParameter("v", parameterType)
                });

                cmd.Parameters["v"].Value = value;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    rowsRead = 0;
                    while (await reader.ReadAsync())
                    {
                        valueRead = reader.GetFieldValue <T>(0);

                        // optional extra test for certain built in types
                        if (typeSpecificReader != null)
                        {
                            Assert.Equal(typeSpecificReader(reader), valueRead);
                            // ReSharper disable once CompareNonConstrainedGenericWithNull
                            Assert.Equal(reader.IsDBNull(0), value == null);
                        }

                        rowsRead++;
                    }
                }
            }
            Assert.Equal(1, rowsRead);
            var valueAsArray = value as Array;

            if (valueAsArray != null)
            {
                var valueReadAsArray = valueRead as Array;
                Assert.NotNull(valueReadAsArray);
                Assert.Equal(valueAsArray.Length, valueReadAsArray.Length);
                for (int i = 0; i < valueAsArray.Length; i++)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    Assert.Equal(valueAsArray.GetValue(i), valueReadAsArray.GetValue(i));
                }
            }
            else
            {
                Assert.Equal(value, valueRead);
            }
        }
Example #6
0
        public async Task BindNull(SpannerDbType parameterType)
        {
            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                var cmd = connection.CreateSelectCommand(
                    "SELECT @v",
                    new SpannerParameterCollection {
                    new SpannerParameter("v", parameterType)
                });

                cmd.Parameters["v"].Value = null;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    Assert.True(await reader.ReadAsync());

                    Assert.True(reader.IsDBNull(0));
                    Assert.Equal(DBNull.Value, reader.GetValue(0));

                    Assert.False(await reader.ReadAsync());
                }
            }
        }
Example #7
0
        private async Task <int> InsertAsync(SpannerParameterCollection values)
        {
            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                values.Add("K", SpannerDbType.String, _lastKey = UniqueString());
                var cmd = connection.CreateInsertCommand("T", values);

                return(await cmd.ExecuteNonQueryAsync());
            }
        }
Example #8
0
        private async Task WriteSampleRowsAsync()
        {
            await _testFixture.EnsureTestDatabaseAsync();

            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                await WriteSampleRowsAsync(connection);
            }
        }
        private async Task WriteSampleRowsAsync()
        {
            await _testFixture.EnsureTestDatabaseAsync();

            if (string.IsNullOrEmpty(_key))
            {
                _key = Guid.NewGuid().ToString();
                using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
                {
                    SpannerCommand insupdate;
                    // 1st update
                    await connection.OpenAsync();

                    using (var tx = await connection.BeginTransactionAsync())
                    {
                        insupdate = connection.CreateInsertOrUpdateCommand(
                            "TX",
                            new SpannerParameterCollection
                        {
                            { "K", SpannerDbType.String, _key },
                            { "StringValue", SpannerDbType.String, Guid.NewGuid().ToString() }
                        });
                        insupdate.Transaction = tx;
                        await insupdate.ExecuteNonQueryAsync();

                        var timestamp = await tx.CommitAsync();

                        _history.Add(
                            new HistoryEntry
                        {
                            Value     = insupdate.Parameters[1].Value.ToString(),
                            Timestamp = timestamp.GetValueOrDefault()
                        });
                    }

                    await Task.Delay(250);

                    // 2nd update
                    using (var tx = await connection.BeginTransactionAsync())
                    {
                        insupdate.Transaction         = tx;
                        insupdate.CommandText         = "UPDATE TX";
                        insupdate.Parameters[1].Value = Guid.NewGuid().ToString();
                        await insupdate.ExecuteNonQueryAsync();

                        var timestamp = await tx.CommitAsync();

                        _history.Add(
                            new HistoryEntry
                        {
                            Value     = insupdate.Parameters[1].Value.ToString(),
                            Timestamp = timestamp.GetValueOrDefault()
                        });
                    }

                    await Task.Delay(250);

                    // 3rd update
                    using (var tx = await connection.BeginTransactionAsync())
                    {
                        insupdate.Transaction         = tx;
                        insupdate.Parameters[1].Value = Guid.NewGuid().ToString();
                        await insupdate.ExecuteNonQueryAsync();

                        var timestamp = await tx.CommitAsync();

                        _history.Add(
                            new HistoryEntry
                        {
                            Value     = insupdate.Parameters[1].Value.ToString(),
                            Timestamp = timestamp.GetValueOrDefault()
                        });
                    }
                }
            }
        }