Slide 1

Slide 1 text

net/http/httptest.Server ͷΞϓϩʔνΛ ςετઓུʹ׆༻͢Δ খࢁ݈Ұ࿠ / GMO PEPABO inc. 2023.6.2 Go Conference 2023 Online 1

Slide 2

Slide 2 text

ࣗݾ঺հ ٕज़෦ɹٕज़ج൫νʔϜ 2018೥ த్ೖࣾ খࢁ ݈Ұ࿠ Ken’ichiro Oyama গ࣮͠༻తͰখ͞ͳOSSΛॻ͘ͷ͕झຯɻ ● GitHub : k1LoW ● Twitter : @k1LoW 2

Slide 3

Slide 3 text

GMOϖύϘʹ͍ͭͯ 3 https://pepabo.com

Slide 4

Slide 4 text

ΞδΣϯμ • ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ • goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ • goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ • ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ • ͓ΘΓʹ 4

Slide 5

Slide 5 text

5 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ

Slide 6

Slide 6 text

6 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ ςεταΠζ • ςετͷ࣮ߦ଎౓ͱܾఆੑʹண໨ͨ͠ςετ෼ྨ • ॻ੶ʮGoogleͷιϑτ΢ΣΞΤϯδχΞϦϯάʯΑΓ • Smallςετʢখςετʣ... ୯ҰϓϩηεʢγϯάϧεϨουʣ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ ͍ɻsleepېࢭɻI/OېࢭɻϒϩοΫېࢭ • Mediumςετʢதςετʣ... ୯ҰϚγϯ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ͍ɻlocalhostҎ֎ͷγε ςϜ΁ͷωοτϫʔΫݺͼग़͠ېࢭ • Largeςετʢେςετʣ... ෳ਺Ϛγϯʹ·͕ͨΔςετ

Slide 7

Slide 7 text

7 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ net/http/httptest.Server • httptest.NewServerͰϩʔΧϧϧʔϓόοΫΞυϨε+γεςϜ͕બΜͩϙʔτͰ଴ͪड͚Δ HTTPαʔόΛىಈͰ͖Δ • HTTPαʔό͸goroutineͰ࣮ߦ͞Ε͍ͯΔ

Slide 8

Slide 8 text

8 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ HTTPΫϥΠΞϯτ-αʔόؒͷςετΛྫʹͨ͠ൺֱ ʢDockerͳͲʣผϓϩηεͰHTTPαʔόΛىಈͯ͠ςετ͢Δ৔߹ͱnet/http/httptest.ServerΛ࢖༻ ͯ͠ςετ͢Δ৔߹Λൺֱ͢Δ • Mediumςετ • ߏ੒ཁૉͷ؅ཧ͸Go Runtime • ؅ཧΛGo Runtimeʹ೚ͤΔ͜ͱ͕Ͱ͖Δ • Mediumςετ • ߏ੒ཁૉͷ؅ཧ͸ϝΠϯϓϩηε • ಠࣗʹ؅ཧΛ͢Δඞཁ͕͋Δ

Slide 9

Slide 9 text

9 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test Knowledge from Savanna 202305 edition” https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34

Slide 10

Slide 10 text

10 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test Knowledge from Savanna 202305 edition” https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34 goroutineϕʔεͷMediumςετ

Slide 11

Slide 11 text

11 ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ goroutineϕʔεͷMediumςετͷϝϦοτ • net/http/httptest.Server͸goroutineϕʔεͷMediumςετΛ࣮ݱͰ͖Δ • ҰൠతͳMediumςετͱൺ΂ͯߏ੒ཁૉͷ؅ཧΛGo Runtime (Scheduler) ʹ೚ͤΔ͜ͱ͕Ͱ ͖ΔͷͰʢܰྔͰʣ҆ఆͨ͠ӡ༻͕ՄೳʹͳΔ • ʮίεύ͕ྑ͍ʯMediumςετͱݴ͑Δ • ผϓϩηεͰ࣮ݱ͢ΔMediumςετͱൺ΂ͯɺಉҰͷGo Runtime্Ͱ࣮ߦͰ͖ΔͨΊ஋ͷ૬ ޓड͚౉͠ͳͲͷ࿈ܞ͕༰қʹ࣮ݱͰ͖Δ • ςετμϒϧʢϞοΫɺελϒɺεύΠͳͲʣͷ࣮૷͕͠΍͍͢

Slide 12

Slide 12 text

goroutineϕʔεͷMediumςετΛ ޿͘׆༻͢Δςετઓུ 12

Slide 13

Slide 13 text

• ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍ • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ • ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦ APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘ • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν 13 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ

Slide 14

Slide 14 text

14 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ςετର৅ͷΞϓϦέʔγϣϯͱςεταΠζͷΠϝʔδ • ઐ༻ͷDBΛ࣋ͪଞͷίϯϙʔωϯτͱ࿈ ಈ͠ͳ͕Βಈ͘APIαʔόΛྫʹͱΔ • Smallςετ͸ςετର৅͕APIαʔό಺ ෦ɻඞཁʹԠͯ͡ςετμϒϧΛ༻ҙɻ • Mediumςετ͸APIαʔόઐ༻DB·Ͱ Λςετσʔλϕʔεͱͯ͠༻ҙɻͦͷ ଞ͸ςετμϒϧΛ༻ҙɻ • Largeςετ͸ίϯϙʔωϯτશମͷಈ ࡞͕ର৅

Slide 15

Slide 15 text

15 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ΞʔΩςΫνϟະ׬ঢ়ଶͷΞϓϦέʔγϣϯ • ։ൃॳظʹ͓͍ͯSmallςετ͸·ͩݻఆతͰ͸ͳ͍

Slide 16

Slide 16 text

• ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍ • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ • ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦ APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘ • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν • ίεύ͕ྑ͍ɻ҆ఆ͍ͯ͠ΔɻςετμϒϧΛ࣮ࢪ͠΍͍͢ 16 goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ

Slide 17

Slide 17 text

17 goroutineϕʔεͷMediumςετͷ ࡞੒Λαϙʔτ͢Δ

Slide 18

Slide 18 text

18 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν • ΞϓϦέʔγϣϯͷ֎ଆΛΈΔͱϓϩτίϧ͸HTTPʹݶΒͣɺgRPCɺDBʢΫΤϦʣɺSMTP ͳͲ͕ߟ͑ΒΕΔ → Mediumςετͷద༻ൣғ͸޿͍ • net/http/httptestύοέʔδҎ֎ʹ΋ύοέʔδΛ༻ҙͯ͠͞ΒʹޮՌΛ্͛Δ

Slide 19

Slide 19 text

19 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/httpstub • ೚ҙͷϨεϙϯεΛฦ͢HTTPαʔόΛ؆୯ʹ૊Έཱͯͯىಈ͢Δ͜ͱ͕Ͱ͖Δ • ͞·͟·ͳ৚݅΍ϨεϙϯεΛઃఆͰ͖ΔͷͰ೚ҙͷHTTPαʔόΛߏஙͰ͖Δ • OpenAPI Spec v3ͷDocumentΛಡΈࠐΜͰϦΫΤετͱϨεϙϯεͷόϦσʔγϣϯ͕Ͱ͖Δ • OpenAPI Spec v3ͷDocumentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢͜ͱ͕Ͱ ͖Δ

Slide 20

Slide 20 text

• ೚ҙͷϨεϙϯεΛฦ͢HTTPαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ 20 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/httpstub

Slide 21

Slide 21 text

• OpenAPI Spec v3 Documentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢ • GET /api/v1/users ͸ examples: ηΫγϣϯͷ 2xx ͳεςʔλεͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢ 21 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/httpstub

Slide 22

Slide 22 text

22 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/grpcstub • ೚ҙͷϨεϙϯεΛฦ͢gRPCαʔόΛ؆୯ʹ૊Έཱͯͯىಈ͢Δ͜ͱ͕Ͱ͖Δ • ͞·͟·ͳ৚݅΍ϨεϙϯεΛઃఆͰ͖ΔͷͰ΄΅೚ҙͷgRPCαʔόΛߏஙͰ͖Δ • protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ϦΫΤετͱϨεϙϯεͷόϦσʔγϣϯ͕Ͱ͖Δ • protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢͜ͱ͕Ͱ͖Δ

Slide 23

Slide 23 text

23 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/grpcstub • ೚ҙͷϨεϙϯεΛฦ͢gRPCαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ

Slide 24

Slide 24 text

• protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢ • routeguide.RouteGuide/GetFeature ͚ͩಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢ 24 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/grpcstub

Slide 25

Slide 25 text

• ೚ҙͷϑΟʔϧυ໊͚ͩಠࣗʹੜ੒ؔ਺Λઃఆͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢ 25 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/grpcstub

Slide 26

Slide 26 text

26 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/smtptest • net/http/httptest.Serverͱಉ͡Α͏ͳ࢖͍উखͰSMTPαʔόΛىಈ͢Δ͜ͱ͕Ͱ͖Δ • ड৴ͨ͠ϝʔϧΛ֬ೝ͢Δ͜ͱ͕Ͱ͖Δ

Slide 27

Slide 27 text

27 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/smtptest • SMTPαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ

Slide 28

Slide 28 text

28 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/smtptest • ड৴ͨ͠ϝʔϧΛ֬ೝ͢Δ͜ͱ͕Ͱ͖Δ

Slide 29

Slide 29 text

29 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ github.com/k1LoW/runn • γφϦΦΛYAMLͰॻ͍ͯͦΕΛ΋ͱʹૢ࡞ʢΦϖϨʔγϣϯʣΛࣗಈԽͰ͖Δ • HTTPɺgRPCɺDatabaseʢSQLʣɺChrome DevTools Protocolɺ೚ҙίϚϯυͷ࣮ߦʢϩʔΧ ϧ/SSHʣͳͲʹରԠ

Slide 30

Slide 30 text

• runn͸1ͭҎ্ͷγφϦΦΛ࣮ߦͯ͠ɺͦΕͧΕͷ੒ޭ/ࣦഊΛΈΔ • 1ͭͷγφϦΦ͸1ͭҎ্ͷεςοϓ͔Βߏ੒͞Ε͍ͯΔ • 1γφϦΦ1YAMLϑΝΠϧʹͳ͍ͬͯΔʢϥϯϒοΫͱݺΜͰ͍Δʣ • γφϦΦ͝ͱʹϥϯφʔͱݺΜͰ͍Δεςοϓ࣮ߦίϯϙʔωϯτΛఆٛ͠ɺͦͷϥϯφʔΛ࢖֤ͬͯε ςοϓΛ࣮ߦ͍ͯ͘͠ • ϥϯφʔʹ͸γφϦΦ಺ʹఆ͕ٛඞཁͳ௨ৗͷϥϯφʔͱɺطʹrunnͰ༻ҙ͍ͯ͠ΔϏϧτΠϯϥϯφʔͷ2 छྨ͕͋Δ • ϥϯφʔ: HTTPϥϯφʔɺDBϥϯφʔɺgRPCϥϯφʔɺCDPϥϯφʔɺSSHϥϯφʔ • ϏϧτΠϯϥϯφʔ: ExecϥϯφʔɺTestϥϯφʔɺDumpϥϯφʔɺIncludeϥϯφʔɺBindϥϯφʔ 30 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ runnͷΞʔΩςΫνϟ

Slide 31

Slide 31 text

31 goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ ϥϯφʔͷઃఆ ม਺ͷઃఆ ɹɹɹɹ֤εςοϓ 0 1 2 3

Slide 32

Slide 32 text

32 More about runn • runnʹ͍ͭͯ͸ɺൃදεϥΠυ΍ZennϒοΫ͕͋ΔͷͰੋඇ͝ཡ͍ͩ͘͞ • https://speakerdeck.com/k1low/stac2022 • https://zenn.dev/k1low/books/runn-cookbook goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ

Slide 33

Slide 33 text

33 ࣮ફͯ͠ײ͡Δ ϝϦοτͱσϝϦοτ

Slide 34

Slide 34 text

• ҰൠతͳMediumςετͱൺ΂Δͱɺಉ͡Goͷίʔυϕʔεʹด͓ͯ͡Γೝ஌ෛՙ͕খ͘͞ײ͡Δ • σʔλϕʔεʹ͍ͭͯ͸ผϓϩηεͳͷͰैདྷ௨Γ • ʮClientΛϞοΫ͢ΔςετʯΑΓ΋ɺຊདྷͷڍಈʹ͍ۙʮServer-ClientͰ࣮ࡍʹ௨৴͢Δςετʯ ͷ΄͏͕௚ײత͔΋ͯ͠ͳ͍ • 1γφϦΦ͋ͨΓͷΧόϨοδ͕େ͖͘ɺ݁ՌϦϑΝΫλϦϯά΁ͷ଱ੑ͕େ͖͘ͳ͍ͬͯΔ • γφϦΦͷ֦ॆ΋YAMLΛॻ͚ͩ͘Ͱྑ͍ɻҰ౓૊ΈࠐΜͰ͠·͑͹ɺ͋ͱ͸YAMLΛॻ͚ͩ͘ʹͳ Δ • γφϦΦ͕OpenAPI SpecϥΠΫͰ͋Δ͜ͱ͔ΒΤϯδχΞʹͱͬͯಡΈ΍͘͢ɺͦͷ··ΦϯϘʔ σΟϯάʹ໾ʹཱͬͨʢAPIར༻ଆ΁ͷઆ໌ͳͲʣɻॻ͖΍͍͢=ಡΈ΍͍͢ 34 ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ ϝϦοτ

Slide 35

Slide 35 text

• Mediumςετ͸Smallςετͱൺ΂Δͱ࣮ߦ͕࣌ؒ௕͍ɻMediumςετ͕༰қʹ֦ ॆͰ͖ΔΑ͏ʹͳͬͨ݁Ռɺશମͷςετ࣮ߦ͕࣌ؒେ͖͘ͳΓ͕ͪ • ςετ΍σʔλϕʔεΛฒྻԽ͢ΔͳͲͯ͠ର߅͍ͯ͠Δ • ΞϓϦέʔγϣϯ֎෦ʹؾܰʹελϒαʔόΛཱͯͯςετ͕Ͱ͖ΔΑ͏ʹͳΔ͜ ͱͰɺΞϓϦέʔγϣϯࣗମͷςετ༰қੑ͕Լ͕Γ͕ͪ • UnitςετΛૄ͔ʹͯ͠͸͍͚ͳ͍ͷ͸໌Β͔ͳͷͰɺΞʔΩςΫνϟະ׬ঢ় ଶͱ͸͍͑ςετ༰қੑ͸୲อ͍ͯ͘͠ 35 ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ σϝϦοτ

Slide 36

Slide 36 text

͓ΘΓʹ 36

Slide 37

Slide 37 text

• ϓϩμΫτͷ৽ن։ൃ࣌ͷΞʔΩςΫνϟͷະ׬ঢ়ଶʹର߅͢Δखஈͱͯ͠ɺgoroutineϕʔεͷ MediumςετΛ޿͘׆༻͢Δํ਑Λऔͬͨ • ࠓޙɺʮن໛͕େ͖͘ͳΔʯʮΞʔΩςΫνϟ͕҆ఆ͢ΔʯͳͲͷཁҼʹΑΓςετߏ੒͕มΘ Δ͜ͱ͸େ͍ʹ͋Γ͏Δ͕ɺগͳ͘ͱ΋ݱ࣌఺ʹ͓͍ͯ͸ѱ͘ͳ͍൑அͩͬͨ 37 ͓ΘΓʹ ͓ΘΓʹ

Slide 38

Slide 38 text

Thank You! Thank You! 38