taosdemoTestTblAlt.py 4.7 KB
Newer Older
P
Ping Xiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import os
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *
import threading
import time


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)

29
        self.numberOfTables = 8
P
Ping Xiao 已提交
30 31
        self.numberOfRecords = 1000000

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

P
Ping Xiao 已提交
48
    def insertDataAndAlterTable(self, threadID):
49 50 51 52 53 54 55
        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
        binPath = buildPath + "/build/bin/"

P
Ping Xiao 已提交
56
        if(threadID == 0):
57 58 59
            print("%staosdemo -y -t %d -n %d -b INT,INT,INT,INT" %
                      (binPath, self.numberOfTables, self.numberOfRecords))
            os.system("%staosdemo -y -t %d -n %d -b INT,INT,INT,INT" %
60
                      (binPath, self.numberOfTables, self.numberOfRecords))
P
Ping Xiao 已提交
61
        if(threadID == 1):
P
Ping Xiao 已提交
62
            time.sleep(2)
P
Ping Xiao 已提交
63
            print("use test")
64 65 66
            max_try = 100
            count = 0
            while (count < max_try):
67 68 69 70 71
                try:
                    tdSql.execute("use test")
                    break
                except Exception as e:
                    tdLog.info("use database test failed")
72 73 74
                    time.sleep(2)
                    count += 1
                    print("try %d times" % count)
75 76
                    continue

P
Ping Xiao 已提交
77
            # check if all the tables have heen created
78 79
            count = 0
            while (count < max_try):
80 81 82 83
                try:
                    tdSql.query("show tables")
                except Exception as e:
                    tdLog.info("show tables test failed")
84 85 86
                    time.sleep(2)
                    count += 1
                    print("try %d times" % count)
87 88
                    continue

P
Ping Xiao 已提交
89
                rows = tdSql.queryRows
P
Ping Xiao 已提交
90 91 92 93 94
                print("number of tables: %d" % rows)
                if(rows == self.numberOfTables):
                    break
                time.sleep(1)
            # check if there are any records in the last created table
95 96
            count = 0
            while (count < max_try):
P
Ping Xiao 已提交
97
                print("query started")
98
                print("try %d times" % count)
99
                try:
100
                    tdSql.query("select * from test.d7")
101 102 103
                except Exception as e:
                    tdLog.info("select * test failed")
                    time.sleep(2)
104 105
                    count += 1
                    print("try %d times" % count)
106 107
                    continue

P
Ping Xiao 已提交
108
                rows = tdSql.queryRows
P
Ping Xiao 已提交
109
                print("number of records: %d" % rows)
P
Ping Xiao 已提交
110 111
                if(rows > 0):
                    break
112
                time.sleep(1)
113

114 115
            print("alter table test.meters add column c10 int")
            tdSql.execute("alter table test.meters add column c10 int")
116 117
            print("insert into test.d7 values (now, 1, 2, 3, 4, 0)")
            tdSql.execute("insert into test.d7 values (now, 1, 2, 3, 4, 0)")
P
Ping Xiao 已提交
118

119
    def run(self):
P
Ping Xiao 已提交
120 121 122 123 124
        tdSql.prepare()

        t1 = threading.Thread(target=self.insertDataAndAlterTable, args=(0, ))
        t2 = threading.Thread(target=self.insertDataAndAlterTable, args=(1, ))

125
        t1.start()
P
Ping Xiao 已提交
126 127 128 129
        t2.start()
        t1.join()
        t2.join()

130 131
        time.sleep(3)

P
Ping Xiao 已提交
132 133 134 135 136 137 138 139 140
        tdSql.query("select count(*) from test.meters")
        tdSql.checkData(0, 0, self.numberOfRecords * self.numberOfTables + 1)

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
141
tdCases.addLinux(__file__, TDTestCase())