| 112 | } |
| 113 | |
| 114 | func TestRecursiveUpdate(t *testing.T) { |
| 115 | syn := newSync() |
| 116 | syn.etcdClient.Delete("/", true) |
| 117 | mustUpdate := func(key, value string) { |
| 118 | if err := syn.Update(key, value); err != nil { |
| 119 | t.Fatalf("unexpected error %s", err.Error()) |
| 120 | } |
| 121 | } |
| 122 | checkNode := func(node *sync.Node, key, value string, children int) { |
| 123 | if node.Key != key { |
| 124 | t.Fatalf("expected key %s has %s", key, node.Key) |
| 125 | } |
| 126 | if node.Value != value { |
| 127 | t.Fatalf("expected value %s has %s", value, node.Value) |
| 128 | } |
| 129 | if len(node.Children) != children { |
| 130 | t.Fatalf("expected to has %d children has %d", children, len(node.Children)) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | mustUpdate("/a/b/c/d/1", "test1") |
| 135 | mustUpdate("/a/b/c/d/2", "test2") |
| 136 | mustUpdate("/a/b/c/d/3", "test3") |
| 137 | mustUpdate("/a/b/e/d/1", "test4") |
| 138 | mustUpdate("/a/b/e/d/2", "test5") |
| 139 | mustUpdate("/a/b/e/d/3", "test6") |
| 140 | |
| 141 | node, err := syn.Fetch("/a") |
| 142 | if err != nil { |
| 143 | t.Fatalf("unexpected error %s", err.Error()) |
| 144 | } |
| 145 | |
| 146 | checkNode(node, "/a", "", 1) |
| 147 | ab := node.Children[0] |
| 148 | checkNode(ab, "/a/b", "", 2) |
| 149 | abc := ab.Children[0] |
| 150 | checkNode(abc, "/a/b/c", "", 1) |
| 151 | abcd := abc.Children[0] |
| 152 | checkNode(abcd, "/a/b/c/d", "", 3) |
| 153 | checkNode(abcd.Children[0], "/a/b/c/d/1", "test1", 0) |
| 154 | checkNode(abcd.Children[1], "/a/b/c/d/2", "test2", 0) |
| 155 | checkNode(abcd.Children[2], "/a/b/c/d/3", "test3", 0) |
| 156 | |
| 157 | abe := ab.Children[1] |
| 158 | checkNode(abe, "/a/b/e", "", 1) |
| 159 | abed := abe.Children[0] |
| 160 | checkNode(abed, "/a/b/e/d", "", 3) |
| 161 | checkNode(abed.Children[0], "/a/b/e/d/1", "test4", 0) |
| 162 | checkNode(abed.Children[1], "/a/b/e/d/2", "test5", 0) |
| 163 | checkNode(abed.Children[2], "/a/b/e/d/3", "test6", 0) |
| 164 | |
| 165 | node, err = syn.Fetch("/a/b/c") |
| 166 | if err != nil { |
| 167 | t.Fatalf("unexpected error %s", err.Error()) |
| 168 | } |
| 169 | checkNode(abc, "/a/b/c", "", 1) |
| 170 | } |
| 171 | |