Skip to content

Commit

Permalink
Fix most common tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gpahal committed Mar 26, 2018
1 parent 6d784d4 commit 47b57f7
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 80 deletions.
27 changes: 1 addition & 26 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,4 @@
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
END OF TERMS AND CONDITIONS
2 changes: 1 addition & 1 deletion pydgraph/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 DGraph Labs, Inc.
# Copyright 2016 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions pydgraph/txn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 DGraph Labs, Inc.
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -177,7 +177,7 @@ def _common_except_commit(self, e):
raise e

def discard(self, timeout=None, metadata=None, credentials=None):
if not self._common_commit():
if not self._common_discard():
return

self._dc.any_client().commit_or_abort(self._ctx, timeout=None, metadata=None, credentials=None)
Expand Down
2 changes: 1 addition & 1 deletion pydgraph/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 DGraph Labs, Inc.
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion scripts/protogen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 DGraph Labs, Inc.
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 DGraph Labs, Inc.
# Copyright 2016 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
50 changes: 36 additions & 14 deletions tests/test_acct_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
import multiprocessing.dummy as mpd
import time
import unittest
from pydgraph import client

from pydgraph.txn import AbortedError

class AcountUpsertIntegrationTestCase(DgraphClientIntegrationTestCase):
from . import helper


class TestAcountUpsert(helper.ClientIntegrationTestCase):
"""Account upsert integration test."""

def setUp(self):
"""Drops existing schema and loads new schema for the test."""
super(AcountUpsertIntegrationTestCase, self).setUp()
super(TestAcountUpsert, self).setUp()
self.concurrency = 5

self.firsts = ['Paul', 'Eric', 'Jack', 'John', 'Martin']
Expand All @@ -43,13 +46,13 @@ def setUp(self):
]
logging.info(len(self.accounts))

_ = self.client.drop_all()
_ = self.client.alter(schema="""
helper.drop_all(self.client)
helper.set_schema(self.client, """
first: string @index(term) .
last: string @index(hash) .
age: int @index(int) .
when: int .
""")
""")

def test_acount_upsert(self):
"""Account upsert integration. Will run upserts concurrently."""
Expand All @@ -63,8 +66,7 @@ def do_upserts(self, account_list, concurrency):
retry_ctr = multiprocessing.Value('i', 0, lock=True)

pool = mpd.Pool(concurrency)
updater = lambda acct: upsert_account(hostname=self.TEST_HOSTNAME,
port=self.TEST_PORT,
updater = lambda acct: upsert_account(addr=self.TEST_SERVER_ADDR,
account=acct,
success_ctr=success_ctr,
retry_ctr=retry_ctr)
Expand Down Expand Up @@ -95,12 +97,16 @@ def assert_changes(self, firsts, accounts):
self.assertTrue('{first}_{last}_{age}'.format(**acct) in account_set)


def upsert_account(hostname, port, account, success_ctr, retry_ctr):
c = client.DgraphClient(hostname, port)
def upsert_account(addr, account, success_ctr, retry_ctr):
c = helper.create_client(addr)
q = '''
{{
acct(func:eq(first, "{first}")) @filter(eq(last, "{last}") AND eq(age, {age})) {{
uid
first
last
age
when
}}
}}'''.format(**account)

Expand All @@ -110,20 +116,22 @@ def upsert_account(hostname, port, account, success_ctr, retry_ctr):
logging.debug('Success: %d Retries: %d', success_ctr.value, retry_ctr.value)
last_update_time = time.time()

txn = c.txn()
try:
txn = c.txn()
result = json.loads(txn.query(q=q).json)
assert len(result['acct']) <= 1, ('Lookup of account %s found '
'multiple accounts' % account)

if not result['acct']:
if account['first'] == 'Paul':
print('creating')
# Account does not exist, so create it
nquads = '''
_:acct <first> "{first}" .
_:acct <last> "{last}" .
_:acct <age> "{age}"^^<xs:int> .
'''.format(**account)
created = txn.mutate(setnquads=nquads)
created = txn.mutate(set_nquads=nquads)
uid = created.uids.get('acct')
assert uid is not None and uid != '', 'Account with uid None/""'
else:
Expand All @@ -135,16 +143,30 @@ def upsert_account(hostname, port, account, success_ctr, retry_ctr):
updatequads = '''
<{0}> <when> "{1:d}"^^<xs:int> .
'''.format(uid, int(time.time()))
updated = txn.mutate(setnquads=updatequads)

prevresult = json.loads(txn.query(q=q).json)['acct']

updated = txn.mutate(set_nquads=updatequads)

txn.commit()

# result = json.loads(c.query(q=q).json)['acct']
# if len(result) > 1 and result[0]['first'] == 'Paul':
# print('1:', prevresult)
# print('2:', updatequads)
# print('3:', result)
# print()

with success_ctr.get_lock():
success_ctr.value += 1
# txn successful, break the loop
return
except grpc._channel._Rendezvous as e:
except (AbortedError, grpc._channel._Rendezvous):
with retry_ctr.get_lock():
retry_ctr.value += 1
# txn failed, retry the loop
finally:
txn.discard()


if __name__ == '__main__':
Expand Down
68 changes: 37 additions & 31 deletions tests/test_bank.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"""
test_bank.py
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

implements test case for running transfer transactions between bank
accounts. runs concurrent writers running multiple transactions in parallel.
"""
import unittest
import grpc
import json
import logging
Expand All @@ -12,22 +21,22 @@
import os
import random
import time
import unittest

from pydgraph import client
from . import test_acct_upsert as integ
from pydgraph.client import DgraphClient
from pydgraph.txn import AbortedError

from . import helper

USERS = 100
CONCURRENCY = 10
XFER_COUNT = 1000


class TestBankXfer(integ.DgraphClientIntegrationTestCase):
class TestBank(helper.ClientIntegrationTestCase):
"""Bank transfer integration test."""

def setUp(self):
"""Drops existing schema and sets up schema for new test."""
super(TestBankXfer, self).setUp()
super(TestBank, self).setUp()
self.concurrency = CONCURRENCY
self.accounts = [
{'bal': 100} for _ in range(USERS)
Expand All @@ -45,7 +54,7 @@ def test_bank_xfer(self):
success_ctr = mp.Value('i', 0, lock=True)
retry_ctr = mp.Value('i', 0, lock=True)
pool = mpd.Pool(self.concurrency)
results = [pool.apply_async(run_xfers, (self.TEST_HOSTNAME, self.TEST_PORT,
results = [pool.apply_async(run_xfers, (self.TEST_SERVER_ADDR,
XFER_COUNT, self.uids,
success_ctr, retry_ctr))
for _ in range(self.concurrency)]
Expand All @@ -56,11 +65,11 @@ def test_bank_xfer(self):

def create_accounts(self):
"""Creates the default set of accounts."""
_ = self.client.drop_all()
_ = self.client.alter(schema="""bal: int .""")
helper.drop_all(self.client)
helper.set_schema(self.client, 'bal: int .')

txn = self.client.txn()
assigned = txn.mutate_obj(setobj=self.accounts)
assigned = txn.mutate(set_obj=self.accounts)
txn.commit()
self.uids.extend(assigned.uids.values())
logging.debug('Created %d accounts', len(assigned.uids))
Expand All @@ -81,7 +90,6 @@ def _looper():

return _looper


def run_total(c, account_uids):
"""Calculates the total ammount in the accounts."""
q = """{{
Expand All @@ -93,16 +101,15 @@ def run_total(c, account_uids):
}}
}}
""".format(uids='", "'.join(account_uids))
resp = c.query(q=q)
resp = c.query(q)
total = json.loads(resp.json)['total']
logging.info("Response: %s", total)
logging.info('Response: %s', total)
assert total[0]['bal'] == 10000


def run_xfers(hostname, port, xfer_count, account_ids, success_ctr, retry_ctr):
def run_xfers(addr, xfer_count, account_ids, success_ctr, retry_ctr):
pname = mpd.current_process().name
log = logging.getLogger('test_bank.run_txfers[%s]' % (pname,))
c = client.DgraphClient(hostname, port)
c = helper.create_client(addr)

while True:
from_acc, to_acc = select_account_pair(account_ids)
Expand All @@ -113,10 +120,10 @@ def run_xfers(hostname, port, xfer_count, account_ids, success_ctr, retry_ctr):
}}
}}""".format(uid1=from_acc, uid2=to_acc)
txn = c.txn()
accounts = load_from_query(txn, query, 'me')
accounts[0]['bal'] += 5
accounts[1]['bal'] -= 5
try:
accounts = load_from_query(txn, query, 'me')
accounts[0]['bal'] += 5
accounts[1]['bal'] -= 5
dump_from_obj(txn, accounts)
with success_ctr.get_lock():
success_ctr.value += 1
Expand All @@ -125,11 +132,12 @@ def run_xfers(hostname, port, xfer_count, account_ids, success_ctr, retry_ctr):
log.info('Runs %d. Aborts: %d', success_ctr.value, retry_ctr.value)
if success_ctr.value >= xfer_count:
break
except grpc._channel._Rendezvous as e:
logging.warn(e)
except (AbortedError, grpc._channel._Rendezvous):
with retry_ctr.get_lock():
retry_ctr.value += 1


with success_ctr.get_lock(), retry_ctr.get_lock():
log.info("success: %d, retries: %d", success_ctr.value, retry_ctr.value)

def select_account_pair(accounts):
"""Selects a pair of accounts at random from accounts ensuring they are not
Expand All @@ -140,15 +148,13 @@ def select_account_pair(accounts):
if not from_acc == to_acc:
return (from_acc, to_acc)


def load_from_query(txn, query, field):
"""Loads a field from the results of a query executed in a txn."""
resp = txn.query(q=query)
resp = txn.query(query)
return json.loads(resp.json)[field]


def dump_from_obj(txn, obj, commit=False):
assigned = txn.mutate_obj(setobj=obj)
assigned = txn.mutate(set_obj=obj)

if not commit:
return assigned
Expand Down
2 changes: 2 additions & 0 deletions tests/test_essentials.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion tests/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 DGraph Labs, Inc.
# Copyright 2016 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_txn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 DGraph Labs, Inc.
# Copyright 2016 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 DGraph Labs, Inc.
# Copyright 2018 Dgraph Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 47b57f7

Please sign in to comment.