用于测试项目的简单时钟模拟

发布于 2024-12-07 09:34:42 字数 402 浏览 0 评论 0原文

考虑测试您刚刚实施的项目。如果它无论如何都使用系统时钟,测试它就会是一个问题。我想到的第一个解决方案是模拟;手动操纵系统时钟来愚弄软件的所有组件,让其相信时间正在按照您希望的方式流逝。您如何实施这样的解决方案?

我的解决方案是: 使用虚拟环境(例如VMWare Player)并安装Linux(我将发行版留给您)并操纵虚拟系统的时钟来创建时间流逝的错觉。唯一的问题是,当你的代码运行时,时钟正在滴答作响。我本人正在寻找一种解决方案,让时间实际上停止,并且除非我告诉它,否则它不会改变。

限制: 您不能限制项目中使用的组件列表,因为它们可能是任何东西。例如,我使用了 MySQL 日期/时间函数,我想愚弄它们而不修改 MySQL 的代码(这太昂贵了,因为你可能最终会编译项目的每个组件)。

Consider testing the project you've just implemented. If it's using the system's clock in anyway, testing it would be an issue. The first solution that comes to mind is simulation; manually manipulate system's clock to fool all the components of your software to believe the time is ticking the way you want it to. How do you implement such a solution?

My solution is:
Using a virtual environment (e.g. VMWare Player) and installing a Linux (I leave the distribution to you) and manipulating virtual system's clock to create the illusion of time passing. The only problem is, clock is ticking as your code is running. Me, myself, am looking for a solution that time will actually stop and it won't change unless I tell it to.

Constraints:
You can't confine the list of components used in project, as they might be anything. For instance I used MySQL date/time functions and I want to fool them without amending MySQL's code in anyway (it's too costy since you might end up compiling every single component of your project).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洒一地阳光 2024-12-14 09:34:42

编写一个小程序,可以在您需要的时候以及您想要的程度更改系统时钟。例如,每一秒将时钟额外更改 59 秒。

小程序应该

  • 要么跟踪它所做的事情,以便可以撤消它,
  • 要么使用网络时间协议将时钟恢复到其旧值(之前参考,记住差异,事后询问,应用差异)。

Write a small program that changes the system clock when you want it, and how much you want it. For example, each second, change the clock an extra 59 seconds.

The small program should

  • Either keep track of what it did, so it can undo it
  • Use the Network Time Protocol to get the clock back to its old value (reference before, remember difference, ask afterwards, apply difference).
情深已缘浅 2024-12-14 09:34:42

从您在评论中的附加解释(也许您冷地将它们添加到您的问题中?),我的想法是:

您可能已经解决了 1 和 1 。 2,但它们与问题相关,即使不是问题。

1) 这是一个网络应用程序,因此您只需要关心服务器的时钟。不要相信任何由客户端控制的时钟。

2)你似乎只需要经过的时间而不是绝对时间。因此,为什么不跟踪服务器请求开始和结束的时间,然后将经过的服务器时间添加回剩余的“时间库”(或任何约束)?

3)就测试而言,您根本不需要关心任何实际的“时钟”。正如 Gilbert Le Blanc 建议的那样,在系统调用周围编写一个包装器,然后可以使用它返回虚拟测试数据。因此,如果您有一个返回当前系统时间的方法 getTime(),您可以将其包装在另一个方法中,或者使用返回任意偏移量的参数重载它。

From your additional explanation in the comments (maybe you cold add them to your question?), my thoughts are:

You may already have solved 1 & 2, but they relate to the problem, if not the question.

1) This is a web application, so you only need to concern yourself with your server's clock. Don't trust any clock that is controlled by the client.

2) You only seem to need elapsed time as opposed to absolute time. Therefore why not keep track of the time at which the server request starts and ends, then add the elapsed server time back on to the remaining 'time-bank' (or whatever the constraint is)?

3) As far as testing goes, you don't need to concern yourself with any actual 'clock' at all. As Gilbert Le Blanc suggests, write a wrapper around your system calls that you can then use to return dummy test data. So if you had a method getTime() which returned the current system time, you could wrap it in another method or overload it with a parameter that returns an arbitrary offset.

温暖的光 2024-12-14 09:34:42

将您的系统调用封装在自己的方法中,您可以用模拟调用替换系统调用进行测试。

编辑以显示示例。

我写 Java 游戏。这是一个简单的 Java Font 类,它将游戏的字体放在一个位置,以防我稍后决定更改字体。

package xxx.xxx.minesweeper.view;

import java.awt.Font;

public class MinesweeperFont {

    protected static final String FONT_NAME = "Comic Sans MS";

    public static Font getBoldFont(int pointSize) {
        return new Font(FONT_NAME, Font.BOLD, pointSize);
    }

}

再次,使用 Java,这里有一个封装系统调用的简单方法。

public static void printConsole(String text) {
    System.out.println(text);
}

将代码中的每个 System.out.println 实例替换为 printConsole,并且您的系统调用仅存在于一个位置。

通过重写或修改封装的方法,您可以测试它们。

Encapsulate your system calls in their own methods, and you can replace the system calls with simulation calls for testing.

Edited to show an example.

I write Java games. Here's a simple Java Font class that puts the font for the game in one place, in case I decide to change the font later.

package xxx.xxx.minesweeper.view;

import java.awt.Font;

public class MinesweeperFont {

    protected static final String FONT_NAME = "Comic Sans MS";

    public static Font getBoldFont(int pointSize) {
        return new Font(FONT_NAME, Font.BOLD, pointSize);
    }

}

Again, using Java, here's a simple method of encapsulating a System call.

public static void printConsole(String text) {
    System.out.println(text);
}

Replace every instance of System.out.println in your code with printConsole, and your system call exists in only one place.

By overriding or modifying the encapsulated methods, you can test them.

能怎样 2024-12-14 09:34:42

另一个解决方案是调试和操作时间函数返回的值,将它们设置为您想要的任何值

Another solution would be to debug and manipulate values returned by time functions to set them to anything you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文