Ejemplo n.º 1
0
        private async Task UnrelatedTransactionsDoNotConflict(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();
            var txn2 = client.NewTransaction();

            var personTxn1 = MintAPerson("Alfred Name");
            var personTxn2 = MintAPerson("Fank Person");

            // Name has term and exact indexes, so these shouldn't clash
            var transactionResultTxn1 = await txn1.Mutate(JsonConvert.SerializeObject(personTxn1));

            AssertResultIsSuccess(transactionResultTxn1);
            personTxn1.Uid = transactionResultTxn1.Value[personTxn1.Uid.Substring(2)];

            var transactionResultTxn2 = await txn2.Mutate(JsonConvert.SerializeObject(personTxn2));

            AssertResultIsSuccess(transactionResultTxn2);
            personTxn2.Uid = transactionResultTxn2.Value[personTxn2.Uid.Substring(2)];

            // Can't see the other result
            var queryByName = await txn2.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn1.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Should().Be("{\"q\":[]}");

            // Cause the mutates can't clash these should be ok
            AssertResultIsSuccess(await txn1.Commit());
            AssertResultIsSuccess(await txn2.Commit());

            // both are there
            queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn1.Name }
            });

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value, personTxn1);

            queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn2.Name }
            });

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value, personTxn2);
        }
Ejemplo n.º 2
0
        private async Task DiscardedTransactionsHaveNoEffect(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();

            var person            = MintAPerson(nameof(DiscardedTransactionsHaveNoEffect));
            var json              = JsonConvert.SerializeObject(person);
            var transactionResult = await txn1.Mutate(json);

            AssertResultIsSuccess(transactionResult);
            person.Uid = transactionResult.Value[person.Uid.Substring(2)];

            await txn1.Discard();

            // Can we see it
            var queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", person.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Should().Be("{\"q\":[]}");

            // Can we see it in the uid (note that uid queries always return the
            // uid ... we are interested if returns the actual person or not)
            var queryByUid = await client.Query(FriendQueries.QueryByUid(person.Uid));

            AssertResultIsSuccess(queryByUid);
            queryByUid.Value.Should().Be($"{{\"q\":[{{\"uid\":\"{person.Uid}\"}}]}}");
        }
Ejemplo n.º 3
0
        private async Task ConflictingTransactionsDontBothSucceed(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();
            var txn2 = client.NewTransaction();

            var personTxn1 = MintAPerson("Bill Person");
            var personTxn2 = MintAPerson("Jane Person");

            var transactionResultTxn1 = await txn1.Mutate(JsonConvert.SerializeObject(personTxn1));

            AssertResultIsSuccess(transactionResultTxn1);
            personTxn1.Uid = transactionResultTxn1.Value[personTxn1.Uid.Substring(2)];

            var transactionResultTxn2 = await txn2.Mutate(JsonConvert.SerializeObject(personTxn2));

            AssertResultIsSuccess(transactionResultTxn2);

            // Name has term and exact indexes, so these clash on 'Person' term
            AssertResultIsSuccess(await txn1.Commit());
            var txn2Result = await txn2.Commit();

            txn2Result.IsFailed.Should().BeTrue();
            // Not 100% sure about this quirk.  Pretty sure this is how is how
            // dgo does it, but I wonder that it should be in an error state?
            txn2.TransactionState.Should().Be(TransactionState.Committed);

            // txn1 is there
            var queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn1.Name }
            });

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value, personTxn1);

            // txn2 had no effect
            queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn2.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Should().Be("{\"q\":[]}");
        }
Ejemplo n.º 4
0
        private async Task QueryWithVars(IDgraphClient client)
        {
            var queryResult = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", Person3.Name }
            });

            AssertResultIsSuccess(queryResult, "Query failed");

            FriendQueries.AssertStringIsPerson(queryResult.Value, Person3);
        }
Ejemplo n.º 5
0
        private async Task ConcurrentUpsertsWorkCorrectly(IDgraphClient client)
        {
            var user2 = MintAUser("User2");

            user2.uid = "_:myBlank";
            var predicate = nameof(User.Username);
            var username  = GraphValue.BuildStringValue(user2.Username);
            var json      = JsonConvert.SerializeObject(user2);

            var tasks = Enumerable.Range(0, 10).
                        Select(i => client.Upsert(
                                   predicate,
                                   username,
                                   json,
                                   "myBlank"));

            var results = await Task.WhenAll(tasks.ToList());

            foreach (var result in results)
            {
                AssertResultIsSuccess(result);
            }

            // Only one upsert should have acually written a value
            results.Select(r => r.Value.existing).Where(exists => !exists).Count().Should().Be(1);

            // Only one uid is ever seen
            var theUids = results.Select(r => r.Value.node.UID).Distinct();

            theUids.Count().Should().Be(1);

            // There's only one copy of user2 in the DB
            var queryResult = await client.QueryWithVars(
                UsersByName,
                new Dictionary <string, string> {
                { "$name", user2.Username }
            });

            AssertResultIsSuccess(queryResult);
            var users = (JArray)JObject.Parse(queryResult.Value) ["q"];

            users.Count.Should().Be(1);
            var user = users[0].ToObject <User>();

            string.Format("0x{0:x}", theUids.First()).Should().Be(user.uid);
            user.Username.Should().Be(user2.Username);
        }