.nib std
nib 0.3.0

module use "std/world.nib"

World

The simulated world a wire runs in: its network, schedule, crashes and seed, as a typed value a wire names with in.

A program that binds Net.Sim, Time.Sim or Random.Sim runs against a world the runtime plays: a network that delays, loses and duplicates messages, a virtual clock, hosts that crash. World is that world's description, as ordinary nib data. A program declares the worlds it runs in as values, and a wire names one after its entry:

use "std/option.nib"
use "std/world.nib"
use "std/time.nib"

module Worlds where
  func short :: -> World.World =
    #{ World.calm() with until_ms: Option.Some(100) }

module Main where
  func main :: -> Int ! Time.Clock =
    let _ = @Time.Clock.sleep(50);
    @Time.Clock.now()

wire W runs Main.main in Worlds.short where Time.Clock = Time.Sim
50

The function a wire names takes nothing, performs nothing and cannot raise, and answers a World.World; the checker refuses anything else by name. So a world is fixed before the run's first step, and a reader of the program sees the world every test runs in. A wire without in runs in World.calm.

nibc --run --world FILE reads a JSON file over the wire's world: its keys are these records' field names, and a key it gives replaces that field. { "seed": 7 } is the wire's world under another seed, which is how a tool sweeps seeds without restating the world.

Data

data Schedule where Fifo; Random

Which ready fibre runs next, for every fibre of the program. Fifo is the runtime's own queue, first come first served, and decides nothing. Random draws each pick from the seed whenever two or more fibres are ready, and journals it as a World.schedule choice, so an order bug the queue hides shows up under some seed and replays from its journal.

let w = #{ World.calm() with schedule: World.Random };
match w.schedule
  Fifo => "the queue"
  Random => "drawn from the seed"
end
drawn from the seed
data Partition where
  from_ms :: Int;
  until_ms :: Int;
  sides :: Vec<Vec<String>>

While the virtual clock is in [from_ms, until_ms), a host on one of sides cannot reach a host on another. A host on no side reaches everyone.

let cut = World.Partition{ from_ms: 500, until_ms: 1500,
                           sides: #[#["10.0.0.1"], #["10.0.0.2", "10.0.0.3"]] };
Vec.size(cut.sides)
2
dataCrash
data Crash where
  node :: String;
  at_ms :: Int;
  restart_ms :: Option.Option<Int>

A host that stops at at_ms and, with restart_ms, starts again. At the crash every fibre of the host's nodes dies where it is, each of its connections closes for both ends, and its listener goes. An effector's cells are outside the host and survive, which is where a program keeps what must outlive a crash; starting the node again is the program's own work. A restart that is not after its crash is refused when the run starts.

use "std/option.nib"
use "std/world.nib"

module Worlds where
  func outage :: -> World.World =
    #{ World.calm() with crashes: #[World.Crash{ node: "10.0.0.1", at_ms: 700, restart_ms: Option.Some(1200) }] }

module Main where
  func main :: -> Int = Vec.size(Worlds.outage().crashes)

wire W runs Main.main in Worlds.outage
1
dataNet
data Net where
  min_delay_ms :: Int;
  max_delay_ms :: Int;
  drop_permille :: Int;
  duplicate_permille :: Int;
  partitions :: Vec<Partition>

What Net.Sim.send decides for each message. Its delay is drawn from [min_delay_ms, max_delay_ms]; out of 1000 sends, drop_permille are lost and duplicate_permille arrive twice; and a message between two hosts a World.Partition separates at the time it is sent is lost without a draw (a connect across one fails with ConnectFailed). A rate outside 0 to 1000, or a delay range that runs backwards, is refused when the run starts.

let w = World.calm();
#{ w with net: #{ w.net with max_delay_ms: 40, drop_permille: 50 } }.net.max_delay_ms
40
dataWorld
data World where
  seed :: Int;
  until_ms :: Option.Option<Int>;
  schedule :: Schedule;
  net :: Net;
  crashes :: Vec<Crash>

The whole of what the runtime reads. seed is the generator every choice the world draws comes from, so one seed is one run. until_ms is the horizon: a run whose virtual clock passes it ends, failed, with a sentence naming it; Option.None runs until the program returns. schedule picks the next ready fibre, net decides each message's fate, and crashes stops hosts.

let w = World.calm();
(w.seed, w.until_ms, Vec.size(w.crashes))
(0, Option.None, 0)

Functions

funccalm
func calm :: -> World

The world a wire without in runs in: seed 0, no horizon, the FIFO queue, every message delivered in 1 ms, nothing lost, nothing crashed. A world is usually this with one field changed.

World.calm().net.max_delay_ms
1