Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from spkrka/krka/notifier
Browse files Browse the repository at this point in the history
Fix change notification for empty DNS result.
  • Loading branch information
pettermahlen authored Aug 9, 2016
2 parents c8bfd1f + 8f8662f commit 3fd6108
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/spotify/dns/AggregatingChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AggregatingChangeNotifier<T> extends AbstractChangeNotifier<T> {
private final List<ChangeNotifier<T>> changeNotifiers;

private volatile Set<T> records = ImmutableSet.of();
private volatile boolean waitingForFirstEvent = true;

/**
* Create a new aggregating {@link ChangeNotifier}.
Expand Down Expand Up @@ -67,7 +68,8 @@ protected void closeImplementation() {
private synchronized void checkChange() {
Set<T> currentRecords = aggregateSet();

if (!currentRecords.equals(records)) {
if (waitingForFirstEvent || !currentRecords.equals(records)) {
waitingForFirstEvent = false;
final ChangeNotification<T> changeNotification =
newChangeNotification(currentRecords, records);
records = currentRecords;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/spotify/dns/DirectChangeNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DirectChangeNotifier<T> extends AbstractChangeNotifier<T>
private final Supplier<Set<T>> recordsSupplier;

private volatile Set<T> records = ImmutableSet.of();
private volatile boolean waitingForFirstEvent = true;
private volatile boolean run = true;

public DirectChangeNotifier(Supplier<Set<T>> recordsSupplier) {
Expand All @@ -52,7 +53,8 @@ public void run() {
}

final Set<T> current = recordsSupplier.get();
if (!current.equals(records)) {
if (waitingForFirstEvent || !current.equals(records)) {
waitingForFirstEvent = false;
final ChangeNotification<T> changeNotification =
newChangeNotification(current, records);
records = current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ServiceResolvingChangeNotifier<T> extends AbstractChangeNotifier<T>
private final ErrorHandler errorHandler;

private volatile Set<T> records = ImmutableSet.of();
private volatile boolean waitingForFirstEvent = true;

private volatile boolean run = true;

Expand Down Expand Up @@ -100,9 +101,11 @@ public void run() {
errorHandler.handle(fqdn, e);
}
log.error(e.getMessage(), e);
fireIfFirstError();
return;
} catch (Exception e) {
log.error(e.getMessage(), e);
fireIfFirstError();
return;
}

Expand All @@ -116,16 +119,25 @@ public void run() {
current = builder.build();
} catch (Exception e) {
log.error(e.getMessage(), e);
fireIfFirstError();
return;
}

if (!current.equals(records)) {
if (waitingForFirstEvent || !current.equals(records)) {
waitingForFirstEvent = false;
final ChangeNotification<T> changeNotification =
newChangeNotification(current, records);
records = current;

fireRecordsUpdated(changeNotification);
}
}

private void fireIfFirstError() {
if (waitingForFirstEvent) {
waitingForFirstEvent = false;
fireRecordsUpdated(newChangeNotification(current(), current()));
}
}
}

66 changes: 66 additions & 0 deletions src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2016 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.spotify.dns;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Test;

import java.util.Set;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class AggregatingChangeNotifierTest {
@Test
public void testEmptySet() throws Exception {
MyNotifier childNotifier = new MyNotifier();
AggregatingChangeNotifier<String> notifier = new AggregatingChangeNotifier<String>(ImmutableList.<ChangeNotifier<String>>of(childNotifier));

ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class);
notifier.setListener(listener, false);

verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class));

childNotifier.set(ImmutableSet.<String>of());

verify(listener, times(1)).onChange(any(ChangeNotifier.ChangeNotification.class));
verifyNoMoreInteractions(listener);

}

private static class MyNotifier extends AbstractChangeNotifier<String> {
private volatile Set<String> records = ImmutableSet.of();

@Override
protected void closeImplementation() {
}

@Override
public Set<String> current() {
return records;
}

public void set(Set<String> records) {
fireRecordsUpdated(newChangeNotification(records, current()));
this.records = records;
}
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/spotify/dns/DnsSrvResolversIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.spotify.dns.statistics.DnsReporter;
import com.spotify.dns.statistics.DnsTimingContext;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -29,6 +28,7 @@
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -62,7 +62,7 @@ public void shouldTrackMetricsWhenToldTo() throws Exception {
resolver.resolve("_spotify-client._tcp.sto.spotify.net");
verify(timingReporter).stop();
verify(reporter, never()).reportFailure(isA(RuntimeException.class));
verify(reporter, never()).reportEmpty();
verify(reporter, times(1)).reportEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public void shouldDoSomethingWithNulls() throws Exception {
sut.setListener(listener, false);
sut.run();

verify(listener, times(1)).onChange(any(ChangeNotifier.ChangeNotification.class));
verifyNoMoreInteractions(listener);
}

Expand All @@ -224,6 +225,7 @@ public void shouldCallErrorHandlerOnResolveErrors() throws Exception {

verify(errorHandler).handle(FQDN, exception);
verifyNoMoreInteractions(f);
verify(listener, times(1)).onChange(any(ChangeNotifier.ChangeNotification.class));
verifyNoMoreInteractions(listener);
}

Expand Down

0 comments on commit 3fd6108

Please sign in to comment.