<commit_before>package main

import (
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"
)

// HTTP ServeMux with an updateable handler so that tests can pass their own
// anonymous functions in to handle requests.
type CDNServeMux struct {
	Name    string
	Port    int
	handler func(w http.ResponseWriter, r *http.Request)
	server  *httptest.Server
}

func (s *CDNServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method == "HEAD" && r.URL.Path == "/" {
		w.Header().Set("PING", "PONG")
		return
	}

	s.handler(w, r)
}

func (s *CDNServeMux) SwitchHandler(h func(w http.ResponseWriter, r *http.Request)) {
	s.handler = h
}

// Start a new server and return the CDNServeMux used.
func StartServer(name string, port int) *CDNServeMux {
	handler := func(w http.ResponseWriter, r *http.Request) {}
	mux := &CDNServeMux{name, port, handler, nil}
	addr := fmt.Sprintf(":%d", port)

	go func() {
		err := StoppableHttpListenAndServe(addr, mux)
		if err != nil {
			log.Fatal(err)
		}
	}()

	log.Printf("Started server on port %d", port)
	return mux
}

func StoppableHttpListenAndServe(addr string, mux *CDNServeMux) error {
	server := httptest.NewUnstartedServer(mux)
	mux.server = server
	l, e := net.Listen("tcp", addr)
	if e != nil {
		log.Fatal(e)
	}
	server.Listener = l
	server.Start()
	return nil
}

func (mux *CDNServeMux) Stop() {
	mux.server.Close()
}

// Return a v4 (random) UUID string.
// This might not be strictly RFC4122 compliant, but it will do. Credit:
// https://groups.google.com/d/msg/golang-nuts/Rn13T6BZpgE/dBaYVJ4hB5gJ<commit_msg>Move CDNServeMux.Stop() and rename var<commit_after>package main

import (
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"
)

// HTTP ServeMux with an updateable handler so that tests can pass their own
// anonymous functions in to handle requests.
type CDNServeMux struct {
	Name    string
	Port    int
	handler func(w http.ResponseWriter, r *http.Request)
	server  *httptest.Server
}

func (s *CDNServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method == "HEAD" && r.URL.Path == "/" {
		w.Header().Set("PING", "PONG")
		return
	}

	s.handler(w, r)
}

func (s *CDNServeMux) SwitchHandler(h func(w http.ResponseWriter, r *http.Request)) {
	s.handler = h
}

func (s *CDNServeMux) Stop() {
	s.server.Close()
}

// Start a new server and return the CDNServeMux used.
func StartServer(name string, port int) *CDNServeMux {
	handler := func(w http.ResponseWriter, r *http.Request) {}
	mux := &CDNServeMux{name, port, handler, nil}
	addr := fmt.Sprintf(":%d", port)

	go func() {
		err := StoppableHttpListenAndServe(addr, mux)
		if err != nil {
			log.Fatal(err)
		}
	}()

	log.Printf("Started server on port %d", port)
	return mux
}

func StoppableHttpListenAndServe(addr string, mux *CDNServeMux) error {
	server := httptest.NewUnstartedServer(mux)
	mux.server = server
	l, e := net.Listen("tcp", addr)
	if e != nil {
		log.Fatal(e)
	}
	server.Listener = l
	server.Start()
	return nil
}

// Return a v4 (random) UUID string.
// This might not be strictly RFC4122 compliant, but it will do. Credit:
// https://groups.google.com/d/msg/golang-nuts/Rn13T6BZpgE/dBaYVJ4hB5gJ
{'fraction_non_alphanumeric': 0.1430314120158585, 'fraction_numerical': 0.006099420555047271, 'mean_word_length': 3.8664688427299705, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1392624', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1273, 'n_words': 331}