2023-10-11 18:11:24 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGroupAdd(t *testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{"listener", "test"})
|
|
|
|
|
|
|
|
group_listener := NewListenerExt(10)
|
|
|
|
group, err := NewNode(ctx, nil, GroupNodeType, 10, nil, group_listener, NewGroupExt(nil))
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
add_subgroup_signal := NewAddSubGroupSignal("test_group")
|
|
|
|
messages := Messages{}
|
|
|
|
messages = messages.Add(ctx, group.ID, group, nil, add_subgroup_signal)
|
|
|
|
fatalErr(t, ctx.Send(messages))
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
resp_1, _, err := WaitForResponse(group_listener.Chan, 10*time.Millisecond, add_subgroup_signal.Id)
|
2023-10-15 15:14:33 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
error_1, is_error := resp_1.(*ErrorSignal)
|
|
|
|
if is_error {
|
|
|
|
t.Fatalf("Error returned: %s", error_1.Error)
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:24 -06:00
|
|
|
user_id := RandID()
|
2023-10-15 15:14:33 -06:00
|
|
|
add_member_signal := NewAddMemberSignal("test_group", user_id)
|
2023-10-11 18:11:24 -06:00
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
messages = Messages{}
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, group.ID, group, nil, add_member_signal)
|
2023-10-11 18:11:24 -06:00
|
|
|
fatalErr(t, ctx.Send(messages))
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
resp_2, _, err := WaitForResponse(group_listener.Chan, 10*time.Millisecond, add_member_signal.Id)
|
2023-10-11 18:11:24 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
error_2, is_error := resp_2.(*ErrorSignal)
|
|
|
|
if is_error {
|
|
|
|
t.Fatalf("Error returned: %s", error_2.Error)
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:24 -06:00
|
|
|
read_signal := NewReadSignal(map[ExtType][]string{
|
2023-10-15 15:14:33 -06:00
|
|
|
GroupExtType: {"sub_groups"},
|
2023-10-11 18:11:24 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
messages = Messages{}
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, group.ID, group, nil, read_signal)
|
2023-10-11 18:11:24 -06:00
|
|
|
fatalErr(t, ctx.Send(messages))
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
response, _, err := WaitForResponse(group_listener.Chan, 10*time.Millisecond, read_signal.Id)
|
2023-10-11 18:11:24 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
read_response := response.(*ReadResultSignal)
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_groups_serialized := read_response.Extensions[GroupExtType]["sub_groups"]
|
2023-10-11 18:11:24 -06:00
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
sub_groups_type, remaining_types, err := DeserializeType(ctx, sub_groups_serialized.TypeStack)
|
|
|
|
fatalErr(t, err)
|
|
|
|
if len(remaining_types) > 0 {
|
|
|
|
t.Fatalf("Types remaining after deserializing subgroups: %d", len(remaining_types))
|
|
|
|
}
|
2023-10-11 18:11:24 -06:00
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
sub_groups_value, remaining, err := DeserializeValue(ctx, sub_groups_type, sub_groups_serialized.Data)
|
|
|
|
fatalErr(t, err)
|
|
|
|
if len(remaining) > 0 {
|
|
|
|
t.Fatalf("Data remaining after deserializing subgroups: %d", len(remaining_types))
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_groups, ok := sub_groups_value.Interface().(map[string][]NodeID)
|
2023-10-11 18:11:24 -06:00
|
|
|
|
|
|
|
if ok != true {
|
2023-10-15 15:14:33 -06:00
|
|
|
t.Fatalf("sub_groups wrong type %s", sub_groups_value.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sub_groups) != 1 {
|
|
|
|
t.Fatalf("sub_groups wrong length %d", len(sub_groups))
|
|
|
|
}
|
|
|
|
|
|
|
|
test_subgroup, exists := sub_groups["test_group"]
|
|
|
|
if exists == false {
|
|
|
|
t.Fatal("test_group not in subgroups")
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
if len(test_subgroup) != 1 {
|
|
|
|
t.Fatalf("test_group wrong size %d/1", len(test_subgroup))
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
if test_subgroup[0] != user_id {
|
|
|
|
t.Fatalf("sub_groups wrong value %s", test_subgroup[0])
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "Read Response: %+v", read_response)
|
|
|
|
}
|