Skip to content

Commit

Permalink
test: 将 db 端口及文件名改为随机
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiSharp committed Oct 31, 2023
1 parent 288c604 commit 4a7c472
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 60 deletions.
7 changes: 4 additions & 3 deletions common/tests/Rpc/Rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const OLD_ENV = process.env;
let server: Server;
beforeAll(() => {
process.env = { ...OLD_ENV }; // Make a copy
process.env.STORAGE_PORT = "8080";
process.env.STORAGE_HOST = "localhost";

});
beforeAll(() => {
const PORT = (Math.random() * (20000 - 10000) + 10000).toFixed();
process.env.STORAGE_PORT = PORT;
server = createServer((socket) => {
const pubSub = new PubSub();
const pubSubConnection = pubSub.create();
Expand Down
8 changes: 4 additions & 4 deletions common/tests/Storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ let db: DataBase;
const storage = new Storage();
beforeAll(() => {
process.env = { ...OLD_ENV }; // Make a copy
process.env.STORAGE_PORT = "8080";
process.env.STORAGE_HOST = "localhost";
process.env.DB_PATH = "tmp/db.json";
});
beforeEach(() => {
const PORT = (Math.random() * (20000 - 10000) + 10000).toFixed();
process.env.STORAGE_PORT = PORT;
process.env.DB_PATH = "tmp/db" + PORT + ".json";
fs.mkdirSync("tmp/", { recursive: true });

server = createServer((socket) => {
Expand All @@ -39,7 +39,7 @@ beforeEach(() => {
afterEach(async () => {
db.db!.close();
server.close();
fs.rmSync("tmp/db.json", { recursive: true, force: true });
fs.rmSync("tmp/" + process.env.DB_PATH, { recursive: true, force: true });
}, 5000);

afterAll(() => {
Expand Down
54 changes: 1 addition & 53 deletions common/tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,4 @@
import { Common, RpcServer } from "@/index";
import { createServer, Server } from "net";
import { DataBase, PubSub, Queue } from "@neo-screeps/storage";
import fs from "fs";
import _ from "lodash";

const OLD_ENV = process.env;
let server: Server;
let db: DataBase;
beforeAll(() => {
process.env = { ...OLD_ENV }; // Make a copy
process.env.STORAGE_PORT = "8080";
process.env.STORAGE_HOST = "localhost";
process.env.DB_PATH = "tmp/db.json";
});
beforeEach(() => {
fs.mkdirSync("tmp/", { recursive: true });

server = createServer((socket) => {
const pubSub = new PubSub();
const pubSubConnection = pubSub.create();
db = new DataBase();
db.loadDb();
const queue = new Queue();
new RpcServer(
socket,
_.extend({}, pubSubConnection.methods, db.toObject(), queue.toObject()),
);
});

server.listen(
parseInt(process.env.STORAGE_PORT!),
process.env.STORAGE_HOST || "localhost",
);
});

afterEach(async () => {
db.db!.close();
server.close();
fs.rmSync("tmp/db.json", { recursive: true, force: true });
}, 5000);

afterAll(() => {
process.env = OLD_ENV; // Restore old environment
fs.rmSync("tmp/", { recursive: true, force: true });
});
import { Common } from "@/index";

test("findPort", async () => {
const common = new Common();
Expand Down Expand Up @@ -73,10 +28,3 @@ test("checkTerrain", async () => {
const result = common.checkTerrain(terrainString, 0, 0, 1);
expect(result).toBe(true);
});

test("getGameTime", () => {
const common = new Common();
common.storage.storageConnect();
const result = common.getGameTime();
expect(result).toBe(true);
});

0 comments on commit 4a7c472

Please sign in to comment.