Skip to content

Commit

Permalink
Fixed a bug where static final groups could be duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Feb 20, 2019
1 parent 707a7b1 commit edfc629
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 48 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ a link to this page somewhere in the documentation/system about section.
## Change Log for the BPjs Library.

### 2019-02-10
* :bug: Instances of the constant sets class `EventSets` now cannot be duplicated even when they are serialized.
* :arrow_up: Improved hash functions for `SyncStatement`s and `ComposableEventSet`s.
* :arrow_up: `ExecutionTrace`s can now return the last event, in addition to returning the last state.
* :arrow_up: More tests.
* :put_litter_in_its_place: Removed duplicate functionality of "all events except" (had two classes that did this).

### 2019-02-10
* :arrow_up: Updated the Tic-Tac-Toe code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public JsEventSet EventSet(String name, Object predicateObj) {
}

public EventSet allExcept( EventSet es ) {
return EventSets.allExcept(es);
return ComposableEventSet.not(es);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/BThreadSyncSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class BThreadSyncSnapshot implements Serializable {
/**
* BSync statement of the BThread at the time of the snapshot.
*/
private SyncStatement bSyncStatement;
private SyncStatement syncStatement;

private transient ContinuationProgramState programState;

Expand All @@ -68,7 +68,7 @@ public BThreadSyncSnapshot(String name, Function entryPoint, Function interruptH
this.entryPoint = entryPoint;
this.interruptHandler = interruptHandler;
this.continuation = continuation;
this.bSyncStatement = bSyncStatement;
this.syncStatement = bSyncStatement;
}

/**
Expand All @@ -82,20 +82,20 @@ public BThreadSyncSnapshot copyWith(Object aContinuation, SyncStatement aStateme
BThreadSyncSnapshot retVal = new BThreadSyncSnapshot(name, entryPoint);
retVal.continuation = aContinuation;
retVal.setInterruptHandler(interruptHandler);
retVal.bSyncStatement = aStatement;
retVal.syncStatement = aStatement;
aStatement.setBthread(retVal);

return retVal;
}

public SyncStatement getSyncStatement() {
return bSyncStatement;
return syncStatement;
}

public void setSyncStatement(SyncStatement stmt) {
bSyncStatement = stmt;
if (bSyncStatement.getBthread() != this) {
bSyncStatement.setBthread(this);
syncStatement = stmt;
if (syncStatement.getBthread() != this) {
syncStatement.setBthread(this);
}
}

Expand Down Expand Up @@ -142,7 +142,7 @@ public ContinuationProgramState getContinuationProgramState() {
@Override
public int hashCode() {
final int prime = 31;
int result = prime * name.hashCode();
int result = prime * Objects.hash(name, syncStatement);
if (continuation != null) {
result ^= getContinuationProgramState().hashCode();
}
Expand All @@ -166,7 +166,9 @@ public boolean equals(Object obj) {
if (!Objects.equals(getName(), other.getName())) {
return false;
}

if ( ! Objects.equals(syncStatement,other.getSyncStatement()) ) {
return false;
}
if (continuation == null) {
return (other.continuation == null);

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/SyncStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,13 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj == this) return true;
if (obj == null) return false;

if (! (obj instanceof SyncStatement)) {
return false;
}

final SyncStatement other = (SyncStatement) obj;
if ( this.isHot() != other.isHot() ) {
return false;
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/eventsets/EventSets.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public boolean contains(BEvent event) {

@Override
public String toString() {
return ("{AllEvents}");
return ("{all}");
}

private Object readResolve() {
return all;
}
};

Expand All @@ -42,19 +46,10 @@ public boolean contains(BEvent event) {
public String toString() {
return "{none}";
}

private Object readResolve() {
return none;
}
};

public final static EventSet allExcept( EventSet es ) {
return new EventSet(){
@Override
public boolean contains(BEvent event) {
return ! es.contains(event);
}

@Override
public String toString() {
return "{ all except " + es.toString() + "}";
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void test1() throws Exception {
final BThreadSyncSnapshot sysState0 = nodes[0].getSystemState().getBThreadSnapshots().iterator().next();
final BThreadSyncSnapshot sysStatei_1 = nodes[i - 1].getSystemState().getBThreadSnapshots().iterator().next();

assertTrue(sysState0.equals(sysStatei_1));
assertTrue("Failed equality for i=" + i, sysState0.equals(sysStatei_1));

assertEquals(nodes[0], nodes[i - 1]);
assertEquals(nodes[1], nodes[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
package il.ac.bgu.cs.bp.bpjs.model.eventsets;

import il.ac.bgu.cs.bp.bpjs.model.BEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
Expand All @@ -39,22 +46,6 @@ public class EventSetsTest {
private static final BEvent EVT_3 = new BEvent("EVT_3");
private static final BEvent EVT_4 = new BEvent("EVT_4");
private static final BEvent EVT_4_SAME = new BEvent("EVT_4");

/**
* Test of allExcept method, of class EventSets.
*/
@Test
public void testAllExcept() {
EventSet es = EventSets.allExcept(EVT_4);

assertFalse( es.contains(EVT_4) );
assertFalse( es.contains(EVT_4_SAME) );
assertTrue( es.contains(EVT_1) );
assertTrue( es.contains(EVT_2) );
assertTrue( es.contains(EVT_3) );

assertTrue( es.toString().contains(EVT_4.getName()) );
}

@Test
public void testAll() {
Expand All @@ -63,7 +54,7 @@ public void testAll() {
assertTrue( EventSets.all.contains(EVT_3) );
assertTrue( EventSets.all.contains(EVT_4) );
assertTrue( EventSets.all.contains(EVT_4_SAME) );
assertTrue( EventSets.all.toString().contains("All") );
assertTrue( EventSets.all.toString().contains("all") );
}

@Test
Expand All @@ -77,4 +68,26 @@ public void testNone() {
assertTrue( EventSets.none.toString().contains("none") );
}

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
List<EventSet> original = Arrays.asList(EventSets.all, EventSets.none);
byte[] byteArr;
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream outs = new ObjectOutputStream(bytes)) {
outs.writeObject(original);
outs.flush();
bytes.flush();
byteArr = bytes.toByteArray();
}

List<EventSet> deSerialized;
try( ByteArrayInputStream inByteStr = new ByteArrayInputStream(byteArr);
ObjectInputStream inObjStream = new ObjectInputStream(inByteStr) ) {
deSerialized = (List<EventSet>) inObjStream.readObject();
}

assertTrue( deSerialized.get(0) == original.get(0) );
assertTrue( deSerialized.get(1) == original.get(1) );

}
}

0 comments on commit edfc629

Please sign in to comment.