Skip to content

Commit

Permalink
[Docs/Clock] Fix Clock Test (MystenLabs#15248)
Browse files Browse the repository at this point in the history
## Description

The Clock test in the docs had diverged from the test in the framework,
(`sui-framework/tests/clock_tests.move`) and so no longer worked.

This PR brings them back in line with each other.

Closes MystenLabs#15226

## Test Plan

:eyes: +

```
sui-framework-tests$ cargo nextest run -- run_sui_framework_tests
```
  • Loading branch information
amnn authored Dec 7, 2023
1 parent f52a5db commit 798dfcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module sui::clock {
#[test_only]
/// Expose the functionality of `create()` (usually only done during
/// genesis) for tests that want to create a Clock.
public fun create_for_testing(ctx: &mut sui::tx_context::TxContext): Clock {
public fun create_for_testing(ctx: &mut TxContext): Clock {
Clock {
id: object::new(ctx),
timestamp_ms: 0,
Expand Down
29 changes: 18 additions & 11 deletions docs/content/guides/developer/sui-101/access-time.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,24 @@ Any transaction that requires access to a `Clock` must go through consensus beca

Transactions that use the clock must accept it as an **immutable reference** (not a mutable reference or value). This prevents contention, as transactions that access the `Clock` can only read it, so do not need to be sequenced relative to each other. Validators refuse to sign transactions that do not meet this requirement and packages that include entry functions that accept a `Clock` or `&mut Clock` fail to publish.

The following functions test `Clock`-dependent code by manually creating (and sharing) a `Clock` object and incrementing its timestamp. This is possible only in test code:
The following functions test `Clock`-dependent code by manually creating a `Clock` object and manipulating its timestamp. This is possible only in test code:

```move
module sui::clock {
#[test_only]
public fun create_for_testing(ctx: &mut TxContext);
#[test_only]
public fun share_for_testing(clock: Clock);
#[test_only]
public fun increment_for_testing(clock: &mut Clock, tick: u64);
#[test_only]
public fun set_for_testing(clock: &mut Clock, timestamp_ms: u64);
#[test_only]
public fun destroy_for_testing(clock: Clock);
}
```

Expand All @@ -67,22 +76,20 @@ The next example presents a simple test that creates a Clock, increments it, and
```move
module example::clock_tests {
use sui::clock::{Self, Clock};
use sui::test_scenario as ts;
use sui::tx_context;
#[test]
fun creating_a_clock_and_incrementing_it() {
let ts = ts::begin(@0x1);
let ctx = ts::ctx(&mut ts);
let ctx = tx_context::dummy();
let clock = clock::create_for_testing(&mut ctx);
clock::create_for_testing(ctx);
clock::increment_for_testing(&mut clock, 42);
assert!(clock::timestamp_ms(&clock) == 42, 1);
let clock = ts::take_shared<Clock>(&ts);
clock::increment_for_testing(&mut clock, 20);
clock::increment_for_testing(&mut clock, 22);
assert!(clock::timestamp_ms(&clock) == 42, 0);
clock::set_for_testing(&mut clock, 50);
assert!(clock::timestamp_ms(&clock) == 50, 1);
ts::return_shared(clock);
ts::end(ts);
clock::destroy_for_testing(clock);
}
}
```
Expand Down

0 comments on commit 798dfcb

Please sign in to comment.