Skip to content

Commit

Permalink
Merge "UT for auto-generated member function __sizeof__()"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Sep 22, 2015
2 parents 56981ef + de88f96 commit ee56280
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/generate/t_py_generator.cc
Expand Up @@ -2272,7 +2272,7 @@ void t_py_generator::generate_py_sizeof(std::ofstream& out,
if (type->is_map()) {
indent(out) <<
"size += sum(map(getsizeof, chain.from_iterable(self." <<
fname << ")))" << endl;
fname << ".iteritems())))" << endl;

} else {
indent(out) <<
Expand Down
12 changes: 10 additions & 2 deletions library/python/pysandesh/test/msg_test.sandesh
Expand Up @@ -10,14 +10,22 @@

systemlog sandesh SystemLogTest {
1: "Async Test";
2: list<i32> list1;
3: map<i32, string> map1;
4: uuid_t uuid1;
}

struct StructObject {
1: "Object";
1: "Object";
2: bool flag1;
3: list<xml> list1;
4: string s1;
}

objectlog sandesh ObjectLogTest {
1: StructObject object;
1: StructObject object;
2: list<StructObject> list_obj1;
3: u64 u64_1;
}

struct StructKeyHint {
Expand Down
59 changes: 59 additions & 0 deletions library/python/pysandesh/test/sandesh_msg_test.py
Expand Up @@ -14,6 +14,8 @@
import socket
import test_utils
import time
import uuid
from itertools import chain

sys.path.insert(1, sys.path[0]+'/../../../python')

Expand Down Expand Up @@ -109,6 +111,63 @@ def test_sandesh_queue_level_drop(self):
aggregate_stats().messages_sent_dropped_queue_level)
# end test_sandesh_queue_level_drop

def test_sandesh_sizeof(self):
# test sizeof SystemLogTest sandesh without setting any members
system_log = SystemLogTest()
exp_system_log_size = sys.getsizeof('Async Test')
self.assertEqual(exp_system_log_size, system_log.__sizeof__())

# test sizeof SystemLogTest by setting list1 and uuid1
l1 = [10, 10934, 34]
uuid1 = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
system_log = SystemLogTest(list1=l1, uuid1=uuid1)
# verify that the size of individual elements in the list is
# accounted while calclulating the size of a list
exp_system_log_size += sys.getsizeof(l1)
exp_system_log_size += sum(map(sys.getsizeof, l1))
exp_system_log_size += sys.getsizeof(uuid1)
self.assertEqual(exp_system_log_size, system_log.__sizeof__())

# test sizeof SystemLogTest by setting list1, uuid1 and map1
m1 = {1: 'collector', 2: 'query-engine', 3: 'opserver'}
system_log = SystemLogTest(list1=l1, map1=m1, uuid1=uuid1)
# verify that the size of individual elements (key, value)
# in the map is accounted which calculating the size of a map
exp_system_log_size += sys.getsizeof(m1)
exp_system_log_size += \
sum(map(sys.getsizeof, chain.from_iterable(m1.iteritems())))
self.assertEqual(exp_system_log_size, system_log.__sizeof__())

# test the size of struct
exp_struct_obj_size = 0
f1 = False
lxml1 = '<sandesh>EOM</sandesh>'
lxml2 = '<test>sizeof</test>'
l1 = [lxml1, lxml2]
struct_obj1 = StructObject(flag1=f1, list1=l1)
exp_struct_obj_size += sys.getsizeof('Object')
exp_struct_obj_size += sys.getsizeof(f1)
exp_struct_obj_size += sys.getsizeof(lxml1)
exp_struct_obj_size += sys.getsizeof(lxml2)
exp_struct_obj_size += sys.getsizeof(l1)
self.assertEqual(exp_struct_obj_size, struct_obj1.__sizeof__())

# test the size of ObjectLogTest
objlog = ObjectLogTest(object=struct_obj1)
exp_objlog_size = sys.getsizeof(struct_obj1)
self.assertEqual(exp_objlog_size, objlog.__sizeof__())

# modify objlog and verify the size
struct_obj2 = StructObject(s1='contrail-collector')
list_obj = [struct_obj1, struct_obj2]
objlog.object = None
objlog.list_obj1 = list_obj
exp_objlog_size = sys.getsizeof(list_obj)
exp_objlog_size += sys.getsizeof(struct_obj1)
exp_objlog_size += sys.getsizeof(struct_obj2)
self.assertEqual(exp_objlog_size, objlog.__sizeof__())
# end test_sandesh_sizeof

# end class SandeshMsgTest

if __name__ == '__main__':
Expand Down

0 comments on commit ee56280

Please sign in to comment.