mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
GET /v1/models?id=a,b answers the rows whose id is one of the listed ids, in the listing's order; an id nothing lists is absent, and more than 100 ids is a 400. Without ?id= the answer is the whole public body, byte for byte, so every OpenAI-compatible caller that lists models is unchanged. The HTTP-over-ZAP gateway path reads the same query.
111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
)
|
|
|
|
// One build is shared by every endpoint, so the readers are concurrent HTTP handlers
|
|
// and the writer is a config reload. This holds the memo to that: run it under
|
|
// -race and neither the held slice nor the held body may be touched while another
|
|
// caller is reading it.
|
|
//
|
|
// It is here because the property is the one a memo can most easily lose and the
|
|
// one a single-threaded test cannot see at all.
|
|
func TestTheHeldCatalogueIsSafeToReadConcurrently(t *testing.T) {
|
|
withCatalog(t, "alpha", "beta", "gamma")
|
|
|
|
var readers, writer sync.WaitGroup
|
|
stop := make(chan struct{})
|
|
|
|
// The writer: reload the config under the readers. Reload re-applies in place
|
|
// under the config's own write lock, stamping changedAt, so the memo's inputs
|
|
// move and rebuilds happen while reads are in flight — without reassigning
|
|
// globalModelConfig, which nothing synchronizes and which is not what is
|
|
// under test here.
|
|
//
|
|
// It waits on its own group, because it runs until the readers are done and so
|
|
// can never be waited on alongside them.
|
|
writer.Go(func() {
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
default:
|
|
if err := GetModelConfig().Reload(); err != nil {
|
|
t.Errorf("reload: %v", err)
|
|
return
|
|
}
|
|
// Reload reads a file. Left unpaced it starves the readers this
|
|
// test exists to run.
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}
|
|
})
|
|
|
|
for i := range 8 {
|
|
readers.Add(1)
|
|
// Each reader is a different signed-in caller, because a listing is
|
|
// annotated with the READER's own standing — which is the write that a
|
|
// shared backing array would let one caller land in another's answer.
|
|
caller := &iam.User{Owner: "hanzo", Name: fmt.Sprintf("caller-%d", i)}
|
|
go func() {
|
|
defer readers.Done()
|
|
for range 200 {
|
|
models := listAvailableModels()
|
|
if len(models) == 0 {
|
|
t.Error("listAvailableModels returned nothing")
|
|
return
|
|
}
|
|
// Read every element: a shared backing array that another caller is
|
|
// writing shows up here and nowhere else.
|
|
for _, m := range models {
|
|
_ = m.ID
|
|
}
|
|
|
|
body, err := modelListing(caller, nil)
|
|
if err != nil {
|
|
t.Errorf("modelListing: %v", err)
|
|
return
|
|
}
|
|
var envelope struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(body, &envelope); err != nil {
|
|
t.Errorf("held body is not valid json: %v", err)
|
|
return
|
|
}
|
|
if len(envelope.Data) == 0 {
|
|
t.Error("held body lists no models")
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
readers.Wait()
|
|
close(stop)
|
|
writer.Wait()
|
|
}
|