package upnp

import (
	"encoding/xml"
	"testing"
)

// Visually verify that property sets are marshalled correctly.
func TestMarshalPropertySet(t *testing.T) {
	b, err := xml.MarshalIndent(&PropertySet{
		Properties: []Property{
			{
				Variable: Variable{
					XMLName: xml.Name{
						Local: "SystemUpdateID",
					},
					Value: "0",
				},
			},
			{
				Variable: Variable{
					XMLName: xml.Name{
						Local: "answerToTheUniverse",
					},
					Value: "42",
				},
			},
		},
		Space: "urn:schemas-upnp-org:event-1-0",
	}, "", "  ")
	t.Log("\n" + string(b))
	if err != nil {
		t.Fatal(err)
	}
}

func TestParseCallbackURLs(t *testing.T) {
	urls := ParseCallbackURLs("<http://hello><http://path>     <http://world>")
	if len(urls) != 3 {
		t.Fatal(len(urls))
	}
}

func TestSubscribeRace(t *testing.T) {
	const n = 100

	e := &Eventing{}
	done := make(chan struct{})

	doSubscribes := func() {
		for i := 0; i < n; i++ {
			e.Subscribe(nil, 10)
		}
		done <- struct{}{}
	}

	go doSubscribes()
	go doSubscribes()
	<-done
	<-done
}
