You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rework the AutoRegister API and add comprehensive integration tests!
I worked on this in pieces over several weekends. First I got carried
away rewriting things and then later I added the tests.
For AutoRegister changes, I didn't like passing in WaitGroup as a
parameter in the API and I felt like it added too much boilerplate to
call the function. I also didn't like breaking standard go conventions
and returning the error in the struct. Later on after I wrote the tests
I also realised the "return only the first registration" API is nice to
use without the default client as well so I made it a client-specific
version.
In order to make testing work easily I exposed the LeaseTTL as a global
variable. I'm now thinking I want to change it to a flag so each
instance of embedportal.Run can have a different TTL. I already had too
many changes going on and I had to stop and commit.
Tools changes: With the AutoRegister changes I ended up making
tools.AutoTLSConf, but I'm not entirely happy with the API yet. To
tools.HTTPServer I added the Listener option mainly so that I could use
a file descriptor listener in the tests. This also allows binding to a
specific IP. Kinda makes the port argument useless if you use it but
it's nice to have the extra option and get the nice shutdown code.
The host, assimilate, and spawn changes are for the most part to use the
new API so this commit compiles. Also in spawn I ended up adding a
context and WaitGroup to the dashboard. I think I need to rework this
some too.
When I wrote the tests I also found and fixed two bugs:
1. In gate.ResolveFlags() the conditions weren't quite right for people
setting the pointers directly in code
2. There was a subtle threading issue in clientLeasor that I discovered
when I made the tests parallel. Because I was trying to cache the
heap pointers myself setting the nextLeasor variable wasn't a thread
safe operation and it was possible to have race issues. The proper
fix is to use sync.Pool, it was a bit complicated to get exactly
right.
Last and most importantly, I did a lot of refinement on the tests before
committing.
It runs one portal server for all of the parallel tests and uses open
ports automatically selected by the OS and even passes those open
sockets to portal via FDs so that there's no thread race issue when we
close it and portal reopens. Lots of interesting careful use of
t.Cleanup.
I also wrote some cool test helpers for subtests using reflection. So
you don't have to manually name the subtests and update the list of
t.Run calls when you add new test functions. It's almost like it's built
in syntax for go test. Also the FreePort function is reusable.
At first I wrote it to get the portal token by writing the state file to
the test temp dir and then repeatedly reading the state file and parsing
the proto until the token appeared. I ended up changing it over to
reading the logs like portal does because I didn't really like polling
the filesystem and using it for communication like that. Also I decided
I want to be able to test the way spawn actually works and I will write
a portal restart test which proves the state file works without actually
parsing it. Unfortunately it ended up longer code. I think I need to
make extracting the token from portal better but I'm not sure how yet.
As for the actual tests: I was careful to cover all the branches of
AutoRegister although it's not perfect yet. I left lots of TODO
comments. I'm really happy with the cleanup sequences and for example
the extra check that unregister happened at the end of the HTTP test. I
really like that I was able to stick to only the public interface and
make such a nice setup.
0 commit comments