import com.minamp.plex.Server; /** Server.pick() after being away: the home address wins again when it answers, quickly. */ public class PreferHomeTest { static int failures = 0; static void check(boolean ok, String what) { System.out.println((ok ? "PASS " : "FAIL ") + what); if (!ok) failures++; } static Server server(String home, String remote) { Server s = new Server(); s.machineId = "abc123"; s.add(home, Server.LOCAL_HTTP); s.add(remote, Server.REMOTE); s.sortConnections(); s.lastGoodUri = remote; // last time we were away return s; } public static void main(String[] a) throws Exception { String home = "http://127.0.0.1:" + a[0], remote = "http://127.0.0.1:" + a[1], dead = "http://127.0.0.1:1"; long t0 = System.currentTimeMillis(); Server.Conn c = server(home, remote).pick(); check(c.kind == Server.LOCAL_HTTP, "back home: home address chosen over last time's remote one (" + (System.currentTimeMillis() - t0) + " ms)"); t0 = System.currentTimeMillis(); c = server(dead, remote).pick(); long ms = System.currentTimeMillis() - t0; check(c.kind == Server.REMOTE && ms < 3500, "still away: remote chosen, home check cost " + ms + " ms"); Server s = server(home, remote); s.lastGoodUri = home; c = s.pick(); check(c.kind == Server.LOCAL_HTTP, "home last time: home again"); System.out.println(failures == 0 ? "ALL PASSED" : failures + " FAILED"); System.exit(failures == 0 ? 0 : 1); } }