CodeSampleX

샘플

google.golang.org/grpc v1.75.1: NewServer

검증된 샘플 — golang google.golang.org/grpc v1.75.1: NewServer. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: grpc.NewServer creates a non-nil gRPC…

sha256:c93f5be3a25c8159066b29bedfdf762f88edc44b26e9634af641244c7e0ae223

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 go

검증 실행 환경

환경 컨트랙트 단계 실행일
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-14

케이스

HOW
목표
verify grpc.NewServer in pkg:golang/google.golang.org/grpc@v1.75.1
패키지
심벌
  • grpc.NewServer
생성일
2026-09-14T21:52:54Z

컨트랙트

  1. grpc.NewServer creates a non-nil gRPC server instance with default options
  2. grpc.NewServer creates a server configured with custom ServerOption values including message size limits and connection timeout
  3. grpc.Server registers services and exposes their metadata through GetServiceInfo
  4. grpc.Server serves requests on a listener and terminates cleanly on GracefulStop

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

소스 아티팩트 내려받기 (tar.gz)

소스

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify grpc.NewServer in pkg:golang/google.golang.org/grpc@v1.75.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/grpc@v1.75.1
Demonstrate these symbols/APIs:
  - grpc.NewServer

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:bbefdcd95f9df16ef75ec27ba0705f4063ef32ebca2417136df529ac3cd794b9","contract":["grpc.NewServer creates a non-nil gRPC server instance with default options","grpc.NewServer creates a server configured with custom ServerOption values including message size limits and connection timeout","grpc.Server registers services and exposes their metadata through GetServiceInfo","grpc.Server serves requests on a listener and terminates cleanly on GracefulStop"],"goal":"verify grpc.NewServer in pkg:golang/google.golang.org/grpc@v1.75.1","kind":"HOW","packages":["pkg:golang/google.golang.org/grpc@v1.75.1"],"schemaVersion":1,"symbols":["grpc.NewServer"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/grpc@v1.75.1"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/grpc@v1.75.1","symbols":["grpc.NewServer"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require google.golang.org/grpc v1.75.1

require (
	golang.org/x/net v0.41.0 // indirect
	golang.org/x/sys v0.33.0 // indirect
	golang.org/x/text v0.26.0 // indirect
	google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect
	google.golang.org/protobuf v1.36.6 // indirect
)
go.sum
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I=
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
go.opentelemetry.io/otel/sdk v1.37.0 h1:ItB0QUqnjesGRvNcmAcU0LyvkVyGJ2xftD29bWdDvKI=
go.opentelemetry.io/otel/sdk v1.37.0/go.mod h1:VredYzxUvuo2q3WRcDnKDjbdvmO0sCzOvVAiY+yUkAg=
go.opentelemetry.io/otel/sdk/metric v1.37.0 h1:90lI228XrB9jCMuSdA0673aubgRobVZFhbjxHHspCPc=
go.opentelemetry.io/otel/sdk/metric v1.37.0/go.mod h1:cNen4ZWfiD37l5NhS+Keb5RXVWZWpRE+9WyVCpbo5ps=
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 h1:pFyd6EwwL2TqFf8emdthzeX+gZE1ElRq3iM8pui4KBY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A=
google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI=
google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
sample.go
package sample

import (
	"context"
	"net"
	"time"

	"google.golang.org/grpc"
)

// CreateServer creates a gRPC server with the given options.
func CreateServer(opts ...grpc.ServerOption) *grpc.Server {
	return grpc.NewServer(opts...)
}

// ServerConfig holds configuration parameters for constructing a gRPC server.
type ServerConfig struct {
	MaxRecvMsgSize    int
	MaxSendMsgSize    int
	ConnectionTimeout time.Duration
}

// CreateConfiguredServer creates a gRPC server configured with the specified buffer and timeout options.
func CreateConfiguredServer(cfg ServerConfig) *grpc.Server {
	opts := []grpc.ServerOption{
		grpc.MaxRecvMsgSize(cfg.MaxRecvMsgSize),
		grpc.MaxSendMsgSize(cfg.MaxSendMsgSize),
		grpc.ConnectionTimeout(cfg.ConnectionTimeout),
	}
	return grpc.NewServer(opts...)
}

// RegisterDummyService registers a mock service description onto the server.
func RegisterDummyService(s *grpc.Server, serviceName string) {
	sd := &grpc.ServiceDesc{
		ServiceName: serviceName,
		HandlerType: (*any)(nil),
		Methods: []grpc.MethodDesc{
			{
				MethodName: "Ping",
				Handler: func(srv any, ctx context.Context, dec func(any) error, interceptor grpc.UnaryServerInterceptor) (any, error) {
					return "pong", nil
				},
			},
		},
		Streams: []grpc.StreamDesc{},
	}
	s.RegisterService(sd, struct{}{})
}

// StartAndStopServer starts the server on the given listener and cleanly stops it.
func StartAndStopServer(s *grpc.Server, lis net.Listener) error {
	errCh := make(chan error, 1)
	go func() {
		errCh <- s.Serve(lis)
	}()

	s.GracefulStop()
	return <-errCh
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify grpc.NewServer in pkg:golang/google.golang.org/grpc@v1.75.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/grpc@v1.75.1"
  ],
  "symbols": [
    "grpc.NewServer"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"net"
	"os"
	"time"

	"google.golang.org/grpc"
	"sample"
)

func main() {
	// Assertion 1: grpc.NewServer creates a non-nil gRPC server instance with default options
	serverDefault := sample.CreateServer()
	if serverDefault == nil {
		fmt.Fprintf(os.Stderr, "expected non-nil server with default options\n")
		os.Exit(1)
	}
	serverDefault.Stop()

	// Assertion 2: grpc.NewServer creates a server configured with custom ServerOption values
	cfg := sample.ServerConfig{
		MaxRecvMsgSize:    1024 * 1024,
		MaxSendMsgSize:    1024 * 1024,
		ConnectionTimeout: 5 * time.Second,
	}
	serverCustom := sample.CreateConfiguredServer(cfg)
	if serverCustom == nil {
		fmt.Fprintf(os.Stderr, "expected non-nil server with custom options\n")
		os.Exit(1)
	}
	defer serverCustom.Stop()

	// Assertion 3: grpc.Server registers services and exposes their metadata through GetServiceInfo
	serviceName := "sample.v1.EchoService"
	sample.RegisterDummyService(serverCustom, serviceName)

	info := serverCustom.GetServiceInfo()
	svcInfo, ok := info[serviceName]
	if !ok {
		fmt.Fprintf(os.Stderr, "service %q not found in GetServiceInfo\n", serviceName)
		os.Exit(1)
	}
	if len(svcInfo.Methods) != 1 || svcInfo.Methods[0].Name != "Ping" {
		fmt.Fprintf(os.Stderr, "expected method Ping in service info, got %+v\n", svcInfo.Methods)
		os.Exit(1)
	}

	// Assertion 4: grpc.Server serves requests on a listener and terminates cleanly on GracefulStop
	lis, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create local listener: %v\n", err)
		os.Exit(1)
	}

	serverToServe := sample.CreateServer()
	sample.RegisterDummyService(serverToServe, serviceName)

	serveErrCh := make(chan error, 1)
	go func() {
		serveErrCh <- serverToServe.Serve(lis)
	}()

	time.Sleep(50 * time.Millisecond)

	serverToServe.GracefulStop()

	select {
	case serveErr := <-serveErrCh:
		if serveErr != nil {
			fmt.Fprintf(os.Stderr, "server Serve returned unexpected error: %v\n", serveErr)
			os.Exit(1)
		}
	case <-time.After(3 * time.Second):
		fmt.Fprintf(os.Stderr, "server failed to stop within timeout\n")
		serverToServe.Stop()
		os.Exit(1)
	}

	var _ *grpc.Server = serverDefault

	fmt.Println("All grpc.NewServer contract assertions passed.")
}

오리진 시더

익명