TestAddressFileReady verifies that when address files ($IPFS_PATH/api and $IPFS_PATH/gateway) are created, the corresponding HTTP servers are ready to accept connections immediately. This prevents race conditions for tools like systemd path units that start services when these files appear.
(t *testing.T)
| 18 | // to accept connections immediately. This prevents race conditions for tools |
| 19 | // like systemd path units that start services when these files appear. |
| 20 | func TestAddressFileReady(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | |
| 23 | t.Run("api file", func(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | h := harness.NewT(t) |
| 26 | node := h.NewNode().Init() |
| 27 | |
| 28 | // Start daemon in background (don't use StartDaemon which waits for API) |
| 29 | res := node.Runner.MustRun(harness.RunRequest{ |
| 30 | Path: node.IPFSBin, |
| 31 | Args: []string{"daemon"}, |
| 32 | RunFunc: (*exec.Cmd).Start, |
| 33 | }) |
| 34 | node.Daemon = res |
| 35 | defer node.StopDaemon() |
| 36 | |
| 37 | // Poll for api file to appear |
| 38 | apiFile := filepath.Join(node.Dir, "api") |
| 39 | var fileExists bool |
| 40 | for range 100 { |
| 41 | if _, err := os.Stat(apiFile); err == nil { |
| 42 | fileExists = true |
| 43 | break |
| 44 | } |
| 45 | time.Sleep(100 * time.Millisecond) |
| 46 | } |
| 47 | require.True(t, fileExists, "api file should be created") |
| 48 | |
| 49 | // Read the api file to get the address |
| 50 | apiAddr, err := node.TryAPIAddr() |
| 51 | require.NoError(t, err) |
| 52 | |
| 53 | // Extract IP and port from multiaddr |
| 54 | ip, err := apiAddr.ValueForProtocol(4) // P_IP4 |
| 55 | require.NoError(t, err) |
| 56 | port, err := apiAddr.ValueForProtocol(6) // P_TCP |
| 57 | require.NoError(t, err) |
| 58 | |
| 59 | // Immediately try to use the API - should work on first attempt |
| 60 | url := "http://" + ip + ":" + port + "/api/v0/id" |
| 61 | resp, err := http.Post(url, "", nil) |
| 62 | require.NoError(t, err, "RPC API should be ready immediately when api file exists") |
| 63 | defer resp.Body.Close() |
| 64 | require.Equal(t, http.StatusOK, resp.StatusCode) |
| 65 | }) |
| 66 | |
| 67 | t.Run("gateway file", func(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | h := harness.NewT(t) |
| 70 | node := h.NewNode().Init() |
| 71 | |
| 72 | // Start daemon in background |
| 73 | res := node.Runner.MustRun(harness.RunRequest{ |
| 74 | Path: node.IPFSBin, |
| 75 | Args: []string{"daemon"}, |
| 76 | RunFunc: (*exec.Cmd).Start, |
| 77 | }) |
nothing calls this directly
no test coverage detected