diff --git a/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy b/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy index 7fd88b45..57136afc 100644 --- a/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy @@ -32,6 +32,10 @@ class PersisterService { timer.schedule({load()} as TimerTask, 1000) } + void stop() { + timer.cancel() + } + private void load() { if (location.exists() && location.isFile()) { def slurper = new JsonSlurper() @@ -118,7 +122,7 @@ class PersisterService { } if (sf instanceof DownloadedFile) { - json.sources = sf.sources.stream().flatMap( {d -> d.toBase64()}).collect(Collectors.toList()) + json.sources = sf.sources.stream().map( {d -> d.toBase64()}).collect(Collectors.toList()) } json diff --git a/core/src/test/groovy/com/muwire/core/files/PersisterServiceSavingTest.groovy b/core/src/test/groovy/com/muwire/core/files/PersisterServiceSavingTest.groovy index 286e4dbb..e83d9e33 100644 --- a/core/src/test/groovy/com/muwire/core/files/PersisterServiceSavingTest.groovy +++ b/core/src/test/groovy/com/muwire/core/files/PersisterServiceSavingTest.groovy @@ -1,7 +1,11 @@ package com.muwire.core.files +import org.junit.After +import org.junit.Before import org.junit.Test +import com.muwire.core.Destinations +import com.muwire.core.DownloadedFile import com.muwire.core.EventBus import com.muwire.core.InfoHash import com.muwire.core.SharedFile @@ -11,27 +15,42 @@ import net.i2p.data.Base32 class PersisterServiceSavingTest { - @Test - void testSaving() { - File f = new File("build.gradle") + File f + FileHasher fh = new FileHasher() + InfoHash ih + SharedFile sf + def fileSource + EventBus eventBus = new EventBus() + File persisted + PersisterService ps + + @Before + void before() { + f = new File("build.gradle") f = f.getCanonicalFile() - FileHasher fh = new FileHasher() - InfoHash ih = fh.hashFile(f) - SharedFile sf = new SharedFile(f, ih) - def fileSource = new Object() { - Map getSharedFiles() { - Map rv = new HashMap<>() - rv.putAt(f, sf) - rv - } - } - - File persisted = new File("persisted") + ih = fh.hashFile(f) + fileSource = new Object() { + Map getSharedFiles() { + Map rv = new HashMap<>() + rv.put(f, sf) + rv + } + } + persisted = new File("persisted") persisted.delete() persisted.deleteOnExit() + } + + @After + void after() { + ps?.stop() + } + + @Test + void testSavingSharedFile() { + sf = new SharedFile(f, ih) - EventBus eventBus = new EventBus() - PersisterService ps = new PersisterService(persisted, eventBus, 100, fileSource) + ps = new PersisterService(persisted, eventBus, 100, fileSource) ps.start() Thread.sleep(1500) @@ -44,5 +63,27 @@ class PersisterServiceSavingTest { assert json.hashList == [Base32.encode(ih.getHashList())] } } + + @Test + void testSavingDownloadedFile() { + Destinations dests = new Destinations() + sf = new DownloadedFile(f, ih, new HashSet([dests.dest1, dests.dest2])) + + ps = new PersisterService(persisted, eventBus, 100, fileSource) + ps.start() + Thread.sleep(1500) + + JsonSlurper jsonSlurper = new JsonSlurper() + persisted.eachLine { + def json = jsonSlurper.parseText(it) + assert json.file == f.toString() + assert json.length == f.length() + assert json.infoHash == Base32.encode(ih.getRoot()) + assert json.hashList == [Base32.encode(ih.getHashList())] + assert json.sources.size() == 2 + assert json.sources.contains(dests.dest1.toBase64()) + assert json.sources.contains(dests.dest2.toBase64()) + } + } }