osTests.cpp 3.7 KB
Newer Older
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 29
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wpointer-arith"

#include "os.h"
30
#include "tlog.h"
31 32 33 34 35

TEST(osTest, osSystem) {
  const char *flags = "UTL FATAL ";
  ELogLevel   level = DEBUG_FATAL;
  int32_t     dflag = 255;  // tsLogEmbedded ? 255 : uDebugFlag
D
dapan1121 已提交
36
  taosPrintTrace(flags, level, dflag, 0);
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
void fileOperateOnFree(void *param) {
  char *    fname = (char *)param;
  TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE);
  printf("On free thread open file\n");
  ASSERT_NE(pFile, nullptr);

  int ret = taosLockFile(pFile);
  printf("On free thread lock file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);

  ret = taosUnLockFile(pFile);
  printf("On free thread unlock file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);

  ret = taosCloseFile(&pFile);
  ASSERT_EQ(ret, 0);
  printf("On free thread close file ret:%d\n", ret);
}
void *fileOperateOnFreeThread(void *param) {
  fileOperateOnFree(param);
  return NULL;
}
void fileOperateOnBusy(void *param) {
  char *    fname = (char *)param;
  TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE);
  printf("On busy thread open file\n");
  ASSERT_NE(pFile, nullptr);

  int ret = taosLockFile(pFile);
  printf("On busy thread lock file ret:%d\n", ret);
  ASSERT_NE(ret, 0);

  ret = taosUnLockFile(pFile);
  printf("On busy thread unlock file ret:%d\n", ret);
#ifdef _TD_DARWIN_64
  ASSERT_EQ(ret, 0);
#else
  ASSERT_NE(ret, 0);
#endif

  ret = taosCloseFile(&pFile);
  printf("On busy thread close file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);
}
void *fileOperateOnBusyThread(void *param) {
  fileOperateOnBusy(param);
  return NULL;
}

TEST(osTest, osFile) {
  char *fname = "./osfiletest1.txt";

  TdFilePtr pOutFD = taosCreateFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  ASSERT_NE(pOutFD, nullptr);
  printf("create file success\n");

  TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE);
  printf("open file\n");
  ASSERT_NE(pFile, nullptr);

  int ret = taosLockFile(pFile);
  printf("lock file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);

  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);

  TdThread thread1, thread2;
  taosThreadCreate(&(thread1), &thattr, fileOperateOnBusyThread, (void *)fname);
  taosThreadAttrDestroy(&thattr);

  taosThreadJoin(thread1, NULL);
  taosThreadClear(&thread1);

  ret = taosUnLockFile(pFile);
  printf("unlock file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);

  ret = taosCloseFile(&pFile);
  printf("close file ret:%d\n", ret);
  ASSERT_EQ(ret, 0);

  taosThreadCreate(&(thread2), &thattr, fileOperateOnFreeThread, (void *)fname);
  taosThreadAttrDestroy(&thattr);

  taosThreadJoin(thread2, NULL);
  taosThreadClear(&thread2);

  //int ret = taosRemoveFile(fname);
  //ASSERT_EQ(ret, 0);
  //printf("remove file success");
}

132
#pragma GCC diagnostic pop