Search in sources :

Example 1 with OsmConnection

use of de.westnordost.osmapi.OsmConnection in project StreetComplete by westnordost.

the class OverpassMapDataDaoTest method testHandleOverpassQuota.

public void testHandleOverpassQuota() throws InterruptedException {
    Provider provider = mock(Provider.class);
    when(provider.get()).thenReturn(mock(OverpassMapDataParser.class));
    OverpassStatus status = new OverpassStatus();
    status.availableSlots = 0;
    status.nextAvailableSlotIn = 2;
    OsmConnection osm = mock(OsmConnection.class);
    when(osm.makeRequest(eq("status"), any(OverpassStatusParser.class))).thenReturn(status);
    when(osm.makeRequest(eq("interpreter"), eq("POST"), eq(false), any(ApiRequestWriter.class), any(OverpassStatusParser.class))).thenThrow(OsmTooManyRequestsException.class);
    final OverpassMapDataDao dao = new OverpassMapDataDao(osm, provider);
    // the dao will call get(), get an exception in return, ask its status
    // then and at least wait for the specified amount of time before calling again
    final boolean[] result = new boolean[1];
    Thread dlThread = new Thread() {

        @Override
        public void run() {
            // assert false because we interrupt the thread further down...
            result[0] = dao.getAndHandleQuota("", null);
        }
    };
    dlThread.start();
    // sleep the wait time: Downloader should not try to call
    // overpass again in this time
    Thread.sleep(status.nextAvailableSlotIn * 1000);
    verify(osm, times(1)).makeRequest(eq("interpreter"), eq("POST"), eq(false), any(ApiRequestWriter.class), any(OverpassStatusParser.class));
    verify(osm, times(1)).makeRequest(eq("status"), any(OverpassStatusParser.class));
    // now we test if dao will call overpass again after that time. It is not really
    // defined when the downloader must call overpass again, lets assume 1.5 secs here and
    // change it when it fails
    Thread.sleep(1500);
    verify(osm, times(2)).makeRequest(eq("interpreter"), eq("POST"), eq(false), any(ApiRequestWriter.class), any(OverpassStatusParser.class));
    verify(osm, times(2)).makeRequest(eq("status"), any(OverpassStatusParser.class));
    // we are done here, interrupt thread (still part of the test though...)
    dlThread.interrupt();
    dlThread.join();
    assertFalse(result[0]);
}
Also used : ApiRequestWriter(de.westnordost.osmapi.ApiRequestWriter) OsmConnection(de.westnordost.osmapi.OsmConnection) Provider(javax.inject.Provider)

Example 2 with OsmConnection

use of de.westnordost.osmapi.OsmConnection in project StreetComplete by westnordost.

the class UserChangesetsDaoTest method testAmount.

public void testAmount() {
    OsmConnection osm = new OsmConnection("https://master.apis.dev.openstreetmap.org/api/0.6/", "StreetComplete test case", null);
    long userId = 12;
    UserDao userDao = new UserDao(osm);
    UserInfo info = userDao.get(userId);
    UserChangesetsDao dao = new UserChangesetsDao(new ChangesetsDao(osm));
    Counter counter = new Counter();
    dao.findAll(counter, userId, new Date(0));
    assertEquals(info.changesetsCount, counter.count);
}
Also used : UserDao(de.westnordost.osmapi.user.UserDao) OsmConnection(de.westnordost.osmapi.OsmConnection) ChangesetsDao(de.westnordost.osmapi.changesets.ChangesetsDao) UserInfo(de.westnordost.osmapi.user.UserInfo) Date(java.util.Date)

Aggregations

OsmConnection (de.westnordost.osmapi.OsmConnection)2 ApiRequestWriter (de.westnordost.osmapi.ApiRequestWriter)1 ChangesetsDao (de.westnordost.osmapi.changesets.ChangesetsDao)1 UserDao (de.westnordost.osmapi.user.UserDao)1 UserInfo (de.westnordost.osmapi.user.UserInfo)1 Date (java.util.Date)1 Provider (javax.inject.Provider)1