Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Feb 9, 2021
1 parent 92738c5 commit 47663c6
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ a link to this page somewhere in the documentation/system about section.
* :arrow_up: Updated docs.
* :arrow_up: `BProgram` uses an override-able protected method to access its global scope, in case you want to intercept these calls.
* :bug: Fixed an issue that caused `toString` for JavaScript objects to crash at certain cases ([#145](https://github.com/bThink-BGU/BPjs/issues/145)).
* :bug: Storage consolidation confilcts have proper `equals` and `hashCode`.

### 2021-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.MapProxy;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static java.util.stream.Collectors.joining;

/**
Expand Down Expand Up @@ -83,6 +84,31 @@ public Conflict(Map<String, Map<BThreadSyncSnapshot, MapProxy.Modification<Objec
public String toString() {
return "[Conflict " + conflicts.keySet().stream().collect( joining(", ", "{","}")) + "]";
}

@Override
public int hashCode() {
int hash = 5;
hash = 13 * hash + Objects.hashCode(this.conflicts);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Conflict other = (Conflict) obj;

return Objects.equals(this.conflicts, other.conflicts);
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* The MIT License
*
* Copyright 2021 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package il.ac.bgu.cs.bp.bpjs.bprogramio;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author michael
*/
public class StreamObjectStubTest {
/**
* Test of toString method, of class StreamObjectStub.
*/
@Test
public void testBasics() {
StreamObjectStub instance = new StreamObjectStub("Test");
String result = instance.toString();
assertTrue( result.contains("Test") );
Set<StreamObjectStub> stubSet = new HashSet<>();
stubSet.add( new StreamObjectStub("A") );
stubSet.add( new StreamObjectStub("B") );
stubSet.add( new StreamObjectStub("C") );
stubSet.add( new StreamObjectStub("B") );
assertEquals( 3, stubSet.size() );
}

/**
* Test of equals method, of class StreamObjectStub.
*/
@Test
public void testEquals() {
Object sut = new StreamObjectStub("sut");
assertFalse( sut.equals(null) );
assertFalse( sut.equals("String") );
assertFalse( sut.equals(new StreamObjectStub("not-sut")) );
assertTrue( sut.equals(new StreamObjectStub("sut")) );
assertTrue( sut.equals(sut) );
}

@Test
public void testSerialization() throws IOException, ClassNotFoundException {

StreamObjectStub sutA = new StreamObjectStub("A");
StreamObjectStub sutB = new StreamObjectStub("B");
StreamObjectStub sutC = new StreamObjectStub("C");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(StreamObjectStub.BP_PROXY);
oos.writeObject( sutA );
oos.writeObject( sutB );
oos.writeObject( sutC );

oos.flush();
oos.close();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);

assertSame( StreamObjectStub.BP_PROXY, ois.readObject() );
Object outA = ois.readObject();

assertEquals( sutA, outA );
assertNotSame( sutA, outA );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.mozilla.javascript.Context;
Expand Down Expand Up @@ -167,6 +168,7 @@ public void testJsSetData() throws InterruptedException, URISyntaxException {

assertTrue( jsSut.contains(BEvent.named("a")) );
assertFalse( jsSut.contains(BEvent.named("b")) );
assertNotEquals( jsSut, "NOT AN EVENT SET" );

} finally {
Context.exit();
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/model/BProgramStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
import il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter;
import il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener;
import il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

Expand Down Expand Up @@ -142,6 +146,7 @@ public void multipleBThreadHarmony_run() {
@Test
public void conflict_run() {
AtomicBoolean called = new AtomicBoolean();
AtomicReference<StorageConflictViolation> scvRef = new AtomicReference<>();
BProgram sut = new ResourceBProgram("bp_store/bpStore_conflict.js");
BProgramRunner rnr = new BProgramRunner();
rnr.addListener(new PrintBProgramRunnerListener());
Expand All @@ -153,12 +158,28 @@ public void assertionFailed(BProgram bp, SafetyViolationTag theFailedAssertion)
StorageConflictViolation scv = (StorageConflictViolation) theFailedAssertion;
StorageConsolidationResult.Conflict conflict = scv.getConflict();
assertEquals( Set.of("c"), conflict.conflicts.keySet() );
scvRef.set(scv);
}

});
rnr.setBProgram(sut);
rnr.run();
assertTrue( called.get() );

assertEquals( scvRef.get(), scvRef.get());
assertNotEquals( scvRef.get(), "FASFASDFSA" );
assertNotEquals( scvRef.get(), null );
StorageConflictViolation scv2 = new StorageConflictViolation(scvRef.get().getConflict(), scvRef.get().getMessage());
StorageConflictViolation scv3 = new StorageConflictViolation(scvRef.get().getConflict(), "LALALA");

StorageConflictViolation scv4 = new StorageConflictViolation(new StorageConsolidationResult.Conflict(new HashMap<>()), "LALddddALA");
Set<StorageConflictViolation> sscv = new HashSet<>();
sscv.add(scvRef.get());
sscv.add(scv2);
sscv.add(scv3);
sscv.add(scv4);

assertEquals(3, sscv.size() );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import il.ac.bgu.cs.bp.bpjs.model.SyncStatement;
import il.ac.bgu.cs.bp.bpjs.model.BEvent;
import il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot;
import java.util.Set;
import static org.junit.Assert.assertTrue;

public class PrioritizedBSyncEventSelectionStrategyTest {

Expand All @@ -23,7 +25,7 @@ public class PrioritizedBSyncEventSelectionStrategyTest {

@Test
public void testSelectableEvents_noBlocking() throws InterruptedException {
PrioritizedBSyncEventSelectionStrategy sut = new PrioritizedBSyncEventSelectionStrategy();
PrioritizedBSyncEventSelectionStrategy sut = new PrioritizedBSyncEventSelectionStrategy(1224l);

BProgramSyncSnapshot bpss = TestUtils.makeBPSS(
new MockBThreadSyncSnapshot(SyncStatement.make().request(Arrays.asList(EVT_4))),
Expand Down Expand Up @@ -92,5 +94,14 @@ public void testSelectableEvents_allBlockedWithExtrnal() {

assertEquals(Collections.singleton(external), sut.selectableEvents(bpss));
}

@Test
public void testSCornerCases() {
PrioritizedBSyncEventSelectionStrategy sut = new PrioritizedBSyncEventSelectionStrategy(500);

BProgramSyncSnapshot bpss = TestUtils.makeBPSS();
assertTrue(sut.selectableEvents(bpss).isEmpty());
bpss.getExternalEvents().add(EVT_1);
assertEquals( Set.of(EVT_1), sut.selectableEvents(bpss) );
}
}

0 comments on commit 47663c6

Please sign in to comment.