Skip to content

Commit

Permalink
Merge branch 'master' into schedule-properly
Browse files Browse the repository at this point in the history
  • Loading branch information
lhstrh authored Feb 25, 2023
2 parents 8587a94 + 30cbc44 commit c039537
Show file tree
Hide file tree
Showing 11 changed files with 1,082 additions and 498 deletions.
90 changes: 90 additions & 0 deletions __tests__/CausalityGraph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Reactor, App, Triggers, Args, Timer, OutPort, InPort, TimeUnit, TimeValue, Origin, Log, LogLevel, Action } from '../src/core/internal';

/* Set a port in startup to get thing going */
class Starter extends Reactor {
public in = new InPort<number>(this);
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in),
new Args(this.in, this.writable(this.out)),
function(this, __in, __out) {
__out.set(4);

}
);
}
}

class R1 extends Reactor {
public in = new InPort<number>(this);
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in),
new Args(this.in, this.writable(this.out)),
function(this, __in, __out) {
let tmp = __in.get()
let out:number = 0
if (tmp) {
out = tmp - 1;
}
if (out) {
__out.set(out)
}
}
)
}
}

class R2 extends Reactor {
public in = new InPort<number>(this);
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in),
new Args(this.in, this.writable(this.out)),
function(this, __in, __out) {
let tmp = __in.get()
let out:number = 0;
if(tmp && tmp == 0) {
this.util.requestStop
} else {
if (tmp) {
__out.set(tmp - 1)
}
}
}
)
}
}

class testApp extends App {
start: Starter
reactor1: R1;
reactor2: R2;

constructor () {
super();
this.start = new Starter(this);
this.reactor1 = new R1(this);
this.reactor2 = new R2(this);
this._connect(this.start.out, this.reactor1.in)
this._connect(this.reactor1.out, this.reactor2.in)
// this tests the accuracy of the CausalityGraph used in the connect function
test('test if adding cyclic dependency is caught', () => {
expect(() => {
this._connect(this.reactor2.out, this.start.in)
}).toThrowError("New connection introduces cycle.")
})
}
}

var app = new testApp()
app._start()
80 changes: 80 additions & 0 deletions __tests__/InvalidMutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Reactor, State, Bank, App, Triggers, Args, Timer, OutPort, InPort, TimeUnit, TimeValue, Origin, Log, LogLevel, Action } from '../src/core/internal';

class Starter extends Reactor {
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.startup),
new Args(this.writable(this.out)),
function(this, __out) {
__out.set(4);

}
);
}

}

class R1 extends Reactor {
public in1 = new InPort<number>(this);
public in2 = new InPort<number>(this);
public out1 = new OutPort<number>(this);
public out2 = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in1),
new Args(this.in1, this.writable(this.out1)),
function(this, __in, __out) {
__out.set(4)
}
)

this.addMutation(
new Triggers(this.in1),
new Args(this.in1, this.in2, this.out1, this.out2),
function(this, __in1, __in2, __out1, __out2) {
test('expect error on creating creating direct feed through', () => {
expect(() => {
this.connect(__in2, __out2)
}).toThrowError("New connection introduces direct feed through.")
})
test('expect error when creating connection outside container', () => {
expect(() => {
this.connect(__out2, __in2)
}).toThrowError("New connection is outside of container.")
})
let R2 = new R1(this.getReactor())
test('expect error on mutation creating race condition on an output port', () => {
expect(() => {
this.connect(R2.out1, __out1)
}).toThrowError("Destination port is already occupied.")
})
test('expect error on spawning and creating loop within a reactor', () => {
expect(() => {
this.connect(R2.out1, R2.in1)
}).toThrowError("New connection introduces cycle.")
})
}
)
}

}

class testApp extends App {
start: Starter
reactor1: R1;

constructor () {
super();
this.start = new Starter(this);
this.reactor1 = new R1(this);
this._connect(this.start.out, this.reactor1.in1)
}
}

var app = new testApp()
app._start()
78 changes: 78 additions & 0 deletions __tests__/SimpleMutation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Reactor, App, Triggers, Args, Timer, OutPort, InPort, TimeUnit, TimeValue, Origin, Log, LogLevel, Action } from '../src/core/internal';

/* Set a port in startup to get thing going */
class Starter extends Reactor {
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.startup),
new Args(this.writable(this.out)),
function(this, __out) {
__out.set(4);

}
);
}

}

class R1 extends Reactor {
public in = new InPort<number>(this);
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in),
new Args(this.in, this.writable(this.out)),
function(this, __in, __out) {
__out.set(4)
}
)
}



}

class R2 extends Reactor {
public in = new InPort<number>(this);
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addMutation(
new Triggers(this.in),
new Args(this.in, this.out),
function(this, __in, __out) {
test('expect error to be thrown', () => {
expect(() => {
this.connect(__out, __in)
}).toThrowError("New connection is outside of container.")
})
}
)
}

}


class testApp extends App {
start: Starter
reactor1: R1;
reactor2: R2;

constructor () {
super();
this.start = new Starter(this);
this.reactor1 = new R1(this);
this.reactor2 = new R2(this);
this._connect(this.start.out, this.reactor1.in)
this._connect(this.reactor1.out, this.reactor2.in)
}
}

var app = new testApp()
app._start()
3 changes: 2 additions & 1 deletion __tests__/SingleEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('SingleEvent', function () {
expect(expect(seTest.logger).toBeInstanceOf(Logger));


expect(seTest.canConnect(seTest.singleEvent.o, seTest.logger.i)).toBe(false);
expect(function(){seTest.canConnect(seTest.singleEvent.o, seTest.logger.i)})
.toThrow(new Error("Destination port is already occupied."))
expect(seTest.canConnect(seTest.logger.i, seTest.singleEvent.o)).toBe(false);

seTest._start();
Expand Down
69 changes: 69 additions & 0 deletions __tests__/disconnect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Reactor, State, Bank, App, Triggers, Args, Timer, OutPort, InPort, TimeUnit, TimeValue, Origin, Log, LogLevel, Action } from '../src/core/internal';

class Starter extends Reactor {
public out = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.startup),
new Args(this.writable(this.out)),
function(this, __out) {
__out.set(4);

}
);
}

}

class R1 extends Reactor {
public in1 = new InPort<number>(this);
public in2 = new InPort<number>(this);
public out1 = new OutPort<number>(this);
public out2 = new OutPort<number>(this);

constructor(parent: Reactor|null) {
super(parent);
this.addReaction(
new Triggers(this.in1),
new Args(this.in1, this.writable(this.out1)),
function(this, __in, __out) {
__out.set(4)
}
)

this.addMutation(
new Triggers(this.in1),
new Args(this.in1, this.in2, this.out2),
function(this, __in1, __in2, __out2) {
let R2 = new R1(this.getReactor())
test('expect that disconnecting an existing connection will not result in an error being thrown', () => {
expect(() => {
this.connect(R2.out2, R2.in2)
this.disconnect(R2.out2, R2.in2)
this.connect(R2.out2, R2.in2)
this.disconnect(R2.out2)
this.connect(R2.out2, R2.in2)
}).not.toThrow();
})
}
)
}

}

class testApp extends App {
start: Starter
reactor1: R1;

constructor () {
super();
this.start = new Starter(this);
this.reactor1 = new R1(this);
this._connect(this.start.out, this.reactor1.in1)
}
}

var app = new testApp()
app._start()
Loading

0 comments on commit c039537

Please sign in to comment.