So I’ve been playing around with Syncthing the past couple weeks. One thing that helped me understand how the internals work was to reset to the first public commit. At around 2500 lines, it’s an easy read. The most recent release is a bit more code… a bit more to read.
The past couple days I’ve been experimenting with launching multiple local Syncthing instances and I think the code for doing that is pretty tidy so I wanted to share:
With the syncthing.py classes you can write a function like this:
def test_rw_rw_copy():
with SyncthingCluster(["rw", "rw"]) as cluster:
cluster.wait_for_connection()
rw1, rw2 = cluster
write_fstree({"test.txt": "hello world"}, rw1.folder)
check_fstree({"test.txt": "hello world"}, rw2.folder)
write_fstree({"test2.txt": "hello morld"}, rw2.folder)
check_fstree({"test2.txt": "hello morld"}, rw1.folder)
Which spins up two syncthing instances each with a read-write folder shared between them. You can even do more complicated stuff like this:
def test_w_r_move():
with SyncthingCluster(["w", "r"]) as cluster:
cluster.wait_for_connection()
w, r = cluster
# deletes must be ignored in the destination folder
r.config["folder"]["ignoreDelete"] = "true"
r.update_config()
source_fstree = {"test.txt": "hello world"}
write_fstree(source_fstree, w.folder)
check_fstree(source_fstree, r.folder)
processes.cmd(
"syncthing_send.py",
"--interval=1",
"--timeout=30s",
f"--port={w.gui_port}",
f"--api-key={w.api_key}",
w.folder,
strict=False
)
assert read_fstree(Path(w.folder)) == {}
assert read_fstree(Path(r.folder)) == source_fstree
# cluster.inspect()
# input("Continue?")
I’m still in the very early stages of my project but in case it doesn’t go anywhere… I at least wanted to share this because it works pretty well for testing different scenarios (imho) and it is helping me gain more confidence in what the different node databases look like at different points in time.