mirror of https://github.com/zlatinb/muwire
persistence of downloaded files and test
parent
495570bd49
commit
3f3588dbe4
|
@ -32,6 +32,10 @@ class PersisterService {
|
||||||
timer.schedule({load()} as TimerTask, 1000)
|
timer.schedule({load()} as TimerTask, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
timer.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
private void load() {
|
private void load() {
|
||||||
if (location.exists() && location.isFile()) {
|
if (location.exists() && location.isFile()) {
|
||||||
def slurper = new JsonSlurper()
|
def slurper = new JsonSlurper()
|
||||||
|
@ -118,7 +122,7 @@ class PersisterService {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sf instanceof DownloadedFile) {
|
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
|
json
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package com.muwire.core.files
|
package com.muwire.core.files
|
||||||
|
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
|
import com.muwire.core.Destinations
|
||||||
|
import com.muwire.core.DownloadedFile
|
||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
import com.muwire.core.InfoHash
|
import com.muwire.core.InfoHash
|
||||||
import com.muwire.core.SharedFile
|
import com.muwire.core.SharedFile
|
||||||
|
@ -11,27 +15,42 @@ import net.i2p.data.Base32
|
||||||
|
|
||||||
class PersisterServiceSavingTest {
|
class PersisterServiceSavingTest {
|
||||||
|
|
||||||
@Test
|
File f
|
||||||
void testSaving() {
|
|
||||||
File f = new File("build.gradle")
|
|
||||||
f = f.getCanonicalFile()
|
|
||||||
FileHasher fh = new FileHasher()
|
FileHasher fh = new FileHasher()
|
||||||
InfoHash ih = fh.hashFile(f)
|
InfoHash ih
|
||||||
SharedFile sf = new SharedFile(f, ih)
|
SharedFile sf
|
||||||
def fileSource = new Object() {
|
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> getSharedFiles() {
|
||||||
Map<File, SharedFile> rv = new HashMap<>()
|
Map<File, SharedFile> rv = new HashMap<>()
|
||||||
rv.putAt(f, sf)
|
rv.put(f, sf)
|
||||||
rv
|
rv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
persisted = new File("persisted")
|
||||||
File persisted = new File("persisted")
|
|
||||||
persisted.delete()
|
persisted.delete()
|
||||||
persisted.deleteOnExit()
|
persisted.deleteOnExit()
|
||||||
|
}
|
||||||
|
|
||||||
EventBus eventBus = new EventBus()
|
@After
|
||||||
PersisterService ps = new PersisterService(persisted, eventBus, 100, fileSource)
|
void after() {
|
||||||
|
ps?.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSavingSharedFile() {
|
||||||
|
sf = new SharedFile(f, ih)
|
||||||
|
|
||||||
|
ps = new PersisterService(persisted, eventBus, 100, fileSource)
|
||||||
ps.start()
|
ps.start()
|
||||||
Thread.sleep(1500)
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue