persistence of downloaded files and test

pull/4/head
Zlatin Balevsky 2018-07-20 23:21:48 +01:00
parent 495570bd49
commit 3f3588dbe4
2 changed files with 63 additions and 18 deletions

View File

@ -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

View File

@ -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")
f = f.getCanonicalFile()
File f
FileHasher fh = new FileHasher()
InfoHash ih = fh.hashFile(f)
SharedFile sf = new SharedFile(f, ih)
def fileSource = new Object() {
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()
ih = fh.hashFile(f)
fileSource = new Object() {
Map<File, SharedFile> getSharedFiles() {
Map<File, SharedFile> rv = new HashMap<>()
rv.putAt(f, sf)
rv.put(f, sf)
rv
}
}
File persisted = new File("persisted")
persisted = new File("persisted")
persisted.delete()
persisted.deleteOnExit()
}
EventBus eventBus = new EventBus()
PersisterService ps = new PersisterService(persisted, eventBus, 100, fileSource)
@After
void after() {
ps?.stop()
}
@Test
void testSavingSharedFile() {
sf = new SharedFile(f, ih)
ps = new PersisterService(persisted, eventBus, 100, fileSource)
ps.start()
Thread.sleep(1500)
@ -45,4 +64,26 @@ class PersisterServiceSavingTest {
}
}
@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())
}
}
}