Skip to content

Commit

Permalink
Merge pull request #1450 from JoshNels/29-git-action-fix
Browse files Browse the repository at this point in the history
29 - Fix test failure in github action
  • Loading branch information
cwerthomni authored Jun 11, 2024
2 parents 7185177 + e5c73e6 commit 126240e
Showing 1 changed file with 119 additions and 114 deletions.
233 changes: 119 additions & 114 deletions test/os/xt/peer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,114 +11,13 @@ describe('Peer', function() {
const {DISPATCHER, EventType} = goog.module.get('os.xt.events');

// mock storage
var storage = function() {
var storageImpl = Object.defineProperties({}, new (function() {
var keys = [];
var values = {};
var eventsSuspended = false;
var fireStorage = function(key, oldValue, newValue) {
if (eventsSuspended) {
return;
}
var event = new Event('storage');
event.key = key;
event.oldValue = oldValue;
event.newValue = newValue;
event.storageArea = storageImpl;
event.url = window.location.href;
window.dispatchEvent(event);
};
this.key = {
configurable: false,
enumerable: false,
writable: false,
value: function(index) {
return keys[index] || null;
}
};
this.setItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key, value) {
value = String(value);
var oldValue = values[key] || null;
var keyIndex = keys.indexOf(key);
if (keyIndex < 0) {
keys.push(key);
}
values[key] = value;
if (oldValue !== value) {
fireStorage(key, oldValue, value);
}
}
};
this.getItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key) {
return values[key];
}
};
this.removeItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key) {
var keyIndex = keys.indexOf(key);
if (keyIndex > -1) {
keys.splice(keyIndex, 1);
}
var oldValue = values[key] || null;
if (oldValue) {
delete values[key];
fireStorage(key, oldValue, null);
}
}
};
this.clear = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
keys = [];
values = {};
fireStorage(null, null, null);
}
};
this.length = {
configurable: false,
enumerable: false,
get: function() {
return keys.length;
}
};
this.suspendEvents = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
eventsSuspended = true;
}
};
this.resumeEvents = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
eventsSuspended = false;
}
};
})());
return storageImpl;
}();

var stringifyStorage = function() {
var storage;

var stringifyStorage = function(store) {
var str = '';
for (var cursor = 0, len = storage.length; cursor < len; cursor++) {
var key = storage.key(cursor);
str += '\n ' + key + ': ' + storage.getItem(key);
for (var cursor = 0, len = store.length; cursor < len; cursor++) {
var key = store.key(cursor);
str += '\n ' + key + ': ' + store.getItem(key);
}
return 'storage: {' + str + (str.length ? '\n' : '') + '}';
};
Expand All @@ -132,6 +31,111 @@ describe('Peer', function() {
}
};

beforeEach(function() {
Peer.PING_INTERVAL = 500;
storage = function() {
var storageImpl = Object.defineProperties({}, new (function() {
var keys = [];
var values = {};
var eventsSuspended = false;
var fireStorage = function(key, oldValue, newValue) {
if (eventsSuspended) {
return;
}
var event = new Event('storage');
event.key = key;
event.oldValue = oldValue;
event.newValue = newValue;
event.storageArea = storageImpl;
event.url = window.location.href;
window.dispatchEvent(event);
};
this.key = {
configurable: false,
enumerable: false,
writable: false,
value: function(index) {
return keys[index] || null;
}
};
this.setItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key, value) {
value = String(value);
var oldValue = values[key] || null;
var keyIndex = keys.indexOf(key);
if (keyIndex < 0) {
keys.push(key);
}
values[key] = value;
if (oldValue !== value) {
fireStorage(key, oldValue, value);
}
}
};
this.getItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key) {
return values[key];
}
};
this.removeItem = {
configurable: false,
enumerable: false,
writable: false,
value: function(key) {
var keyIndex = keys.indexOf(key);
if (keyIndex > -1) {
keys.splice(keyIndex, 1);
}
var oldValue = values[key] || null;
if (oldValue) {
delete values[key];
fireStorage(key, oldValue, null);
}
}
};
this.clear = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
keys = [];
values = {};
fireStorage(null, null, null);
}
};
this.length = {
configurable: false,
enumerable: false,
get: function() {
return keys.length;
}
};
this.suspendEvents = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
eventsSuspended = true;
}
};
this.resumeEvents = {
configurable: false,
enumerable: false,
writable: false,
value: function() {
eventsSuspended = false;
}
};
})());
return storageImpl;
}();
});

describe('mock storage', function() {
it('adds a key and sets the value', function() {
Expand Down Expand Up @@ -867,8 +871,9 @@ describe('Peer', function() {
a.setTitle('alice');
a.init();
var peerIsReady = jasmine.createSpy('peerIsReady');
a.waitForPeer('b').addCallback(peerIsReady);
a.waitForPeer('c').addCallback(peerIsReady);
var timedOut = jasmine.createSpy('timedOut');
var deferredB = a.waitForPeer('b').addCallback(peerIsReady).addErrback(timedOut);
var deferredC = a.waitForPeer('c').addCallback(peerIsReady).addErrback(timedOut);

var b = new Peer(storage);
b.setId('b');
Expand All @@ -889,8 +894,8 @@ describe('Peer', function() {
});

waitsFor(function() {
return peerIsReady.calls.length === 1;
}, 'peer to become available', Peer.PING_INTERVAL);
return deferredB.hasFired();
}, 'peer b to become available');

runs(function() {
expect(peerIsReady.calls.length).toBe(1);
Expand All @@ -907,8 +912,8 @@ describe('Peer', function() {
});

waitsFor(function() {
return peerIsReady.calls.length === 2;
}, 'peer to become available');
return deferredC.hasFired();
}, 'peer c to become available');

runs(function() {
expect(peerIsReady.calls.length).toBe(2);
Expand Down Expand Up @@ -942,7 +947,7 @@ describe('Peer', function() {
runs(function() {
expect(storage.length).toBe(0);
if (storage.length > 0) {
console.log('freakin\' lingerers, man:\n' + stringifyStorage());
console.log('lingerers:\n' + stringifyStorage(storage));
}
});
});
Expand Down

0 comments on commit 126240e

Please sign in to comment.