use of aQute.bnd.service.repository.ResourceRepository.Listener in project bnd by bndtools.
the class ResourceRepoTest method testMultipleDownloads.
public void testMultipleDownloads() throws Exception {
final Semaphore s = new Semaphore(0);
final AtomicInteger downloads = new AtomicInteger();
ResourceDescriptor rd = create("jar/osgi.jar");
repoImpl.add("x", rd);
final Semaphore done = new Semaphore(0);
DownloadListener l = new DownloadListener() {
@Override
public void success(File file) throws Exception {
done.release();
}
@Override
public void failure(File file, String reason) throws Exception {
System.out.println("failure! " + file + " " + reason);
}
@Override
public boolean progress(File file, int percentage) throws Exception {
return false;
}
};
repoImpl.addListener(new Listener() {
@Override
public void events(ResourceRepositoryEvent... events) throws Exception {
for (ResourceRepositoryEvent event : events) {
if (event.type == TYPE.START_DOWNLOAD) {
System.out.println("trying to acquire s");
s.acquire();
System.out.println("got it");
downloads.incrementAndGet();
}
}
}
});
File f1 = repoImpl.getResource(rd.id, l);
File f2 = repoImpl.getResource(rd.id, l);
assertFalse(f1.isFile());
assertFalse(f2.isFile());
assertEquals(0, downloads.get());
s.release();
done.acquire(2);
assertTrue(f1.isFile());
assertTrue(f2.isFile());
assertTrue(f1.equals(f2));
assertEquals(1, downloads.get());
}
use of aQute.bnd.service.repository.ResourceRepository.Listener in project bnd by bndtools.
the class ResourceRepoTest method testEvents.
public void testEvents() throws Exception {
final AtomicInteger adds = new AtomicInteger();
final AtomicInteger removes = new AtomicInteger();
final AtomicInteger starts = new AtomicInteger();
final AtomicInteger ends = new AtomicInteger();
final AtomicInteger errors = new AtomicInteger();
repoImpl.addListener(new Listener() {
@Override
public void events(ResourceRepositoryEvent... events) throws Exception {
for (ResourceRepositoryEvent event : events) {
switch(event.type) {
case ADD:
adds.incrementAndGet();
break;
case END_DOWNLOAD:
ends.incrementAndGet();
break;
case ERROR:
errors.incrementAndGet();
break;
case REMOVE:
removes.incrementAndGet();
break;
case START_DOWNLOAD:
starts.incrementAndGet();
break;
default:
errors.incrementAndGet();
break;
}
}
}
});
ResourceDescriptor rd = create("jar/osgi.jar");
repoImpl.add("x", rd);
assertEquals(1, adds.get());
assertEquals(0, removes.get());
repoImpl.delete(null, rd.id);
;
assertEquals(1, adds.get());
assertEquals(1, removes.get());
repoImpl.add("x", rd);
File f = repoImpl.getResource(rd.id);
assertEquals(2, adds.get());
assertEquals(1, starts.get());
assertEquals(1, ends.get());
assertEquals(0, errors.get());
}
Aggregations