Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CodenameOne/src/com/codename1/location/GeofenceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ private synchronized void purgeExpired() {
boolean saveFences = false;
boolean saveActive = false;
boolean saveActiveFences = false;
for (Map.Entry<String, Long> time : times.entrySet()) {
for (Iterator<Map.Entry<String, Long>> it = times.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Long> time = it.next();
if (time.getValue() > 0L && time.getValue() < now) {
times.remove(time.getKey());
it.remove();
if (!saveFences && fences.containsKey(time.getKey())) {
saveFences = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ void isBubbleRecognizesBubbleId() {
assertFalse(manager.isBubble("not-bubble"));
}

@FormTest
void updatePurgesExpiredGeofencesWithoutConcurrentModification() throws Exception {
Geofence first = createGeofence("expired-1", 0.0, 0.0, 50, -1L);
Geofence second = createGeofence("expired-2", 0.0, 0.0, 50, -1L);
manager.add(first, second);
Map<String, Long> expirations = new HashMap<String, Long>();
long expiredAt = System.currentTimeMillis() - 1000L;
expirations.put(first.getId(), expiredAt);
expirations.put(second.getId(), expiredAt);
setPrivateField("expiryTimes", expirations);

assertDoesNotThrow(() -> manager.update(1000));
assertFalse(manager.asMap().containsKey(first.getId()));
assertFalse(manager.asMap().containsKey(second.getId()));
}

@FormTest
public void testGeofenceManagerListener() {
// Instantiate the listener
Expand All @@ -147,6 +163,13 @@ public void testGeofenceManagerListener() {
assertTrue(MyGeofenceListener.calledEnter, "onEntered should delegate to registered listener");
}


private void setPrivateField(String name, Object value) throws Exception {
Field field = GeofenceManager.class.getDeclaredField(name);
field.setAccessible(true);
field.set(manager, value);
}

private Geofence createGeofence(String id, double lat, double lng, int radius, long expiration) {
Location location = new Location(lat, lng);
return new Geofence(id, location, radius, expiration);
Expand Down
Loading